home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 1: Comms & Networking / Almathera Ten on Ten - Disc 1: Comms & Networking.iso / amiga-useful / perl / src / doio.c < prev    next >
C/C++ Source or Header  |  1995-05-04  |  63KB  |  2,943 lines

  1. /* $RCSfile: doio.c,v $$Revision: 4.0.1.6 $$Date: 92/06/11 21:08:16 $
  2.  *
  3.  *    Copyright (c) 1991, Larry Wall
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  * $Log:    doio.c,v $
  9.  * Revision 4.0.1.6  92/06/11  21:08:16  lwall
  10.  * patch34: some systems don't declare h_errno extern in header files
  11.  * 
  12.  * Revision 4.0.1.5  92/06/08  13:00:21  lwall
  13.  * patch20: some machines don't define ENOTSOCK in errno.h
  14.  * patch20: new warnings for failed use of stat operators on filenames with \n
  15.  * patch20: wait failed when STDOUT or STDERR reopened to a pipe
  16.  * patch20: end of file latch not reset on reopen of STDIN
  17.  * patch20: seek(HANDLE, 0, 1) went to eof because of ancient Ultrix workaround
  18.  * patch20: fixed memory leak on system() for vfork() machines
  19.  * patch20: get*by* routines now return something useful in a scalar context
  20.  * patch20: h_errno now accessible via $?
  21.  * 
  22.  * Revision 4.0.1.4  91/11/05  16:51:43  lwall
  23.  * patch11: prepared for ctype implementations that don't define isascii()
  24.  * patch11: perl mistook some streams for sockets because they return mode 0 too
  25.  * patch11: reopening STDIN, STDOUT and STDERR failed on some machines
  26.  * patch11: certain perl errors should set EBADF so that $! looks better
  27.  * patch11: truncate on a closed filehandle could dump
  28.  * patch11: stats of _ forgot whether prior stat was actually lstat
  29.  * patch11: -T returned true on NFS directory
  30.  * 
  31.  * Revision 4.0.1.3  91/06/10  01:21:19  lwall
  32.  * patch10: read didn't work from character special files open for writing
  33.  * patch10: close-on-exec wrongly set on system file descriptors
  34.  * 
  35.  * Revision 4.0.1.2  91/06/07  10:53:39  lwall
  36.  * patch4: new copyright notice
  37.  * patch4: system fd's are now treated specially
  38.  * patch4: added $^F variable to specify maximum system fd, default 2
  39.  * patch4: character special files now opened with bidirectional stdio buffers
  40.  * patch4: taintchecks could improperly modify parent in vfork()
  41.  * patch4: many, many itty-bitty portability fixes
  42.  * 
  43.  * Revision 4.0.1.1  91/04/11  17:41:06  lwall
  44.  * patch1: hopefully straightened out some of the Xenix mess
  45.  * 
  46.  * Revision 4.0  91/03/20  01:07:06  lwall
  47.  * 4.0 baseline.
  48.  * 
  49.  */
  50.  
  51. #include "EXTERN.h"
  52. #include "perl.h"
  53.  
  54. #ifdef HAS_SOCKET
  55. #include <sys/socket.h>
  56. #include <netdb.h>
  57. #ifndef ENOTSOCK
  58. #include <net/errno.h>
  59. #endif
  60. #endif
  61.  
  62. #ifdef HAS_SELECT
  63. #ifdef I_SYS_SELECT
  64. #ifndef I_SYS_TIME
  65. #include <sys/select.h>
  66. #endif
  67. #endif
  68. #endif
  69.  
  70. #ifdef HOST_NOT_FOUND
  71. extern int h_errno;
  72. #endif
  73.  
  74. #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
  75. #include <sys/ipc.h>
  76. #ifdef HAS_MSG
  77. #include <sys/msg.h>
  78. #endif
  79. #ifdef HAS_SEM
  80. #include <sys/sem.h>
  81. #endif
  82. #ifdef HAS_SHM
  83. #include <sys/shm.h>
  84. #endif
  85. #endif
  86.  
  87. #ifdef I_PWD
  88. #include <pwd.h>
  89. #endif
  90. #ifdef I_GRP
  91. #include <grp.h>
  92. #endif
  93. #ifdef I_UTIME
  94. #include <utime.h>
  95. #endif
  96. #ifdef I_FCNTL
  97. #include <fcntl.h>
  98. #endif
  99. #ifdef I_SYS_FILE
  100. #include <sys/file.h>
  101. #endif
  102.  
  103. int laststatval = -1;
  104. int laststype = O_STAT;
  105.  
  106. static char* warn_nl = "Unsuccessful %s on filename containing newline";
  107.  
  108. bool
  109. do_open(stab,name,len)
  110. STAB *stab;
  111. register char *name;
  112. int len;
  113. {
  114.     FILE *fp;
  115.     register STIO *stio = stab_io(stab);
  116.     char *myname = savestr(name);
  117.     int result;
  118.     int fd;
  119.     int writing = 0;
  120.     char mode[3];        /* stdio file mode ("r\0" or "r+\0") */
  121.     FILE *saveifp = Nullfp;
  122.     FILE *saveofp = Nullfp;
  123.     char savetype = ' ';
  124.  
  125.     mode[0] = mode[1] = mode[2] = '\0';
  126.     name = myname;
  127.     forkprocess = 1;        /* assume true if no fork */
  128.     while (len && isSPACE(name[len-1]))
  129.     name[--len] = '\0';
  130.     if (!stio)
  131.     stio = stab_io(stab) = stio_new();
  132.     else if (stio->ifp) {
  133.     fd = fileno(stio->ifp);
  134.     if (stio->type == '-')
  135.         result = 0;
  136.     else if (fd <= maxsysfd) {
  137.         saveifp = stio->ifp;
  138.         saveofp = stio->ofp;
  139.         savetype = stio->type;
  140.         result = 0;
  141.     }
  142.     else if (stio->type == '|')
  143.         result = mypclose(stio->ifp);
  144.     else if (stio->ifp != stio->ofp) {
  145.         if (stio->ofp) {
  146.         result = fclose(stio->ofp);
  147.         fclose(stio->ifp);    /* clear stdio, fd already closed */
  148.         }
  149.         else
  150.         result = fclose(stio->ifp);
  151.     }
  152.     else
  153.         result = fclose(stio->ifp);
  154.     if (result == EOF && fd > maxsysfd)
  155.         fprintf(stderr,"Warning: unable to close filehandle %s properly.\n",
  156.           stab_ename(stab));
  157.     stio->ofp = stio->ifp = Nullfp;
  158.     }
  159.     if (*name == '+' && len > 1 && name[len-1] != '|') {    /* scary */
  160.     mode[1] = *name++;
  161.     mode[2] = '\0';
  162.     --len;
  163.     writing = 1;
  164.     }
  165.     else  {
  166.     mode[1] = '\0';
  167.     }
  168.     stio->type = *name;
  169.     if (*name == '|') {
  170.     /*SUPPRESS 530*/
  171.     for (name++; isSPACE(*name); name++) ;
  172. #ifdef TAINT
  173.     taintenv();
  174.     taintproper("Insecure dependency in piped open");
  175. #endif
  176.     fp = mypopen(name,"w");
  177.     writing = 1;
  178.     }
  179.     else if (*name == '>') {
  180. #ifdef TAINT
  181.     taintproper("Insecure dependency in open");
  182. #endif
  183.     name++;
  184.     if (*name == '>') {
  185.         mode[0] = stio->type = 'a';
  186.         name++;
  187.     }
  188.     else
  189.         mode[0] = 'w';
  190.     writing = 1;
  191.     if (*name == '&') {
  192.       duplicity:
  193.         name++;
  194.         while (isSPACE(*name))
  195.         name++;
  196.         if (isDIGIT(*name))
  197.         fd = atoi(name);
  198.         else {
  199.         stab = stabent(name,FALSE);
  200.         if (!stab || !stab_io(stab)) {
  201. #ifdef EINVAL
  202.             errno = EINVAL;
  203. #endif
  204.             goto say_false;
  205.         }
  206.         if (stab_io(stab) && stab_io(stab)->ifp) {
  207.             fd = fileno(stab_io(stab)->ifp);
  208.             if (stab_io(stab)->type == 's')
  209.             stio->type = 's';
  210.         }
  211.         else
  212.             fd = -1;
  213.         }
  214.         if (!(fp = fdopen(fd = dup(fd),mode))) {
  215.         close(fd);
  216.         }
  217.     }
  218.     else {
  219.         while (isSPACE(*name))
  220.         name++;
  221.         if (strEQ(name,"-")) {
  222.         fp = stdout;
  223.         stio->type = '-';
  224.         }
  225.         else  {
  226.         fp = fopen(name,mode);
  227.         }
  228.     }
  229.     }
  230.     else {
  231.     if (*name == '<') {
  232.         mode[0] = 'r';
  233.         name++;
  234.         while (isSPACE(*name))
  235.         name++;
  236.         if (*name == '&')
  237.         goto duplicity;
  238.         if (strEQ(name,"-")) {
  239.         fp = stdin;
  240.         stio->type = '-';
  241.         }
  242.         else
  243.         fp = fopen(name,mode);
  244.     }
  245.     else if (name[len-1] == '|') {
  246. #ifdef TAINT
  247.         taintenv();
  248.         taintproper("Insecure dependency in piped open");
  249. #endif
  250.         name[--len] = '\0';
  251.         while (len && isSPACE(name[len-1]))
  252.         name[--len] = '\0';
  253.         /*SUPPRESS 530*/
  254.         for (; isSPACE(*name); name++) ;
  255.         fp = mypopen(name,"r");
  256.         stio->type = '|';
  257.     }
  258.     else {
  259.         stio->type = '<';
  260.         /*SUPPRESS 530*/
  261.         for (; isSPACE(*name); name++) ;
  262.         if (strEQ(name,"-")) {
  263.         fp = stdin;
  264.         stio->type = '-';
  265.         }
  266.         else
  267.         fp = fopen(name,"r");
  268.     }
  269.     }
  270.     if (!fp) {
  271.     if (dowarn && stio->type == '<' && index(name, '\n'))
  272.         warn(warn_nl, "open");
  273.     Safefree(myname);
  274.     goto say_false;
  275.     }
  276.     Safefree(myname);
  277.     if (stio->type &&
  278.       stio->type != '|' && stio->type != '-') {
  279.     if (fstat(fileno(fp),&statbuf) < 0) {
  280.         (void)fclose(fp);
  281.         goto say_false;
  282.     }
  283.     if (S_ISSOCK(statbuf.st_mode))
  284.         stio->type = 's';    /* in case a socket was passed in to us */
  285. #ifdef HAS_SOCKET
  286.     else if (
  287. #ifdef S_IFMT
  288.         !(statbuf.st_mode & S_IFMT)
  289. #else
  290.         !statbuf.st_mode
  291. #endif
  292.     ) {
  293.         int buflen = sizeof tokenbuf;
  294.         if (getsockname(fileno(fp), tokenbuf, &buflen) >= 0
  295.         || errno != ENOTSOCK)
  296.         stio->type = 's'; /* some OS's return 0 on fstat()ed socket */
  297.                 /* but some return 0 for streams too, sigh */
  298.     }
  299. #endif
  300.     }
  301.     if (saveifp) {        /* must use old fp? */
  302.     fd = fileno(saveifp);
  303.     if (saveofp) {
  304.         fflush(saveofp);        /* emulate fclose() */
  305.         if (saveofp != saveifp) {    /* was a socket? */
  306.         fclose(saveofp);
  307.         if (fd > 2)
  308.             Safefree(saveofp);
  309.         }
  310.     }
  311.     if (fd != fileno(fp)) {
  312.         int pid;
  313.         STR *str;
  314.  
  315.         dup2(fileno(fp), fd);
  316.         str = afetch(fdpid,fileno(fp),TRUE);
  317.         pid = str->str_u.str_useful;
  318.         str->str_u.str_useful = 0;
  319.         str = afetch(fdpid,fd,TRUE);
  320.         str->str_u.str_useful = pid;
  321.         fclose(fp);
  322.  
  323.     }
  324.     fp = saveifp;
  325.     clearerr(fp);
  326.     }
  327. #if defined(HAS_FCNTL) && defined(F_SETFD)
  328.     fd = fileno(fp);
  329.     fcntl(fd,F_SETFD,fd > maxsysfd);
  330. #endif
  331.     stio->ifp = fp;
  332.     if (writing) {
  333.     if (stio->type == 's'
  334.       || (stio->type == '>' && S_ISCHR(statbuf.st_mode)) ) {
  335.         if (!(stio->ofp = fdopen(fileno(fp),"w"))) {
  336.         fclose(fp);
  337.         stio->ifp = Nullfp;
  338.         goto say_false;
  339.         }
  340.     }
  341.     else
  342.         stio->ofp = fp;
  343.     }
  344.     return TRUE;
  345.  
  346. say_false:
  347.     stio->ifp = saveifp;
  348.     stio->ofp = saveofp;
  349.     stio->type = savetype;
  350.     return FALSE;
  351. }
  352.  
  353. FILE *
  354. nextargv(stab)
  355. register STAB *stab;
  356. {
  357.     register STR *str;
  358. #ifndef FLEXFILENAMES
  359.     int filedev;
  360.     int fileino;
  361. #endif
  362.     int fileuid;
  363.     int filegid;
  364.     static int filemode = 0;
  365.     static int lastfd;
  366.     static char *oldname;
  367.  
  368.     if (!argvoutstab)
  369.     argvoutstab = stabent("ARGVOUT",TRUE);
  370.     if (filemode & (S_ISUID|S_ISGID)) {
  371.     fflush(stab_io(argvoutstab)->ifp);  /* chmod must follow last write */
  372. #ifdef HAS_FCHMOD
  373.     (void)fchmod(lastfd,filemode);
  374. #else
  375.     (void)chmod(oldname,filemode);
  376. #endif
  377.     }
  378.     filemode = 0;
  379.     while (alen(stab_xarray(stab)) >= 0) {
  380.     str = ashift(stab_xarray(stab));
  381.     str_sset(stab_val(stab),str);
  382.     STABSET(stab_val(stab));
  383.     oldname = str_get(stab_val(stab));
  384.     if (do_open(stab,oldname,stab_val(stab)->str_cur)) {
  385.         if (inplace) {
  386. #ifdef TAINT
  387.         taintproper("Insecure dependency in inplace open");
  388. #endif
  389.         if (strEQ(oldname,"-")) {
  390.             str_free(str);
  391.             defoutstab = stabent("STDOUT",TRUE);
  392.             return stab_io(stab)->ifp;
  393.         }
  394. #ifndef FLEXFILENAMES
  395.         filedev = statbuf.st_dev;
  396.         fileino = statbuf.st_ino;
  397. #endif
  398.         filemode = statbuf.st_mode;
  399.         fileuid = statbuf.st_uid;
  400.         filegid = statbuf.st_gid;
  401.         if (!S_ISREG(filemode)) {
  402.             warn("Can't do inplace edit: %s is not a regular file",
  403.               oldname );
  404.             do_close(stab,FALSE);
  405.             str_free(str);
  406.             continue;
  407.         }
  408.         if (*inplace) {
  409. #ifdef SUFFIX
  410.             add_suffix(str,inplace);
  411. #else
  412.             str_cat(str,inplace);
  413. #endif
  414. #ifndef FLEXFILENAMES
  415.             if (stat(str->str_ptr,&statbuf) >= 0
  416.               && statbuf.st_dev == filedev
  417.               && statbuf.st_ino == fileino ) {
  418.             warn("Can't do inplace edit: %s > 14 characters",
  419.               str->str_ptr );
  420.             do_close(stab,FALSE);
  421.             str_free(str);
  422.             continue;
  423.             }
  424. #endif
  425. #ifdef HAS_RENAME
  426. #ifndef DOSISH
  427.             if (rename(oldname,str->str_ptr) < 0) {
  428.             warn("Can't rename %s to %s: %s, skipping file",
  429.               oldname, str->str_ptr, strerror(errno) );
  430.             do_close(stab,FALSE);
  431.             str_free(str);
  432.             continue;
  433.             }
  434. #else
  435.             do_close(stab,FALSE);
  436.             (void)unlink(str->str_ptr);
  437.             (void)rename(oldname,str->str_ptr);
  438.             do_open(stab,str->str_ptr,stab_val(stab)->str_cur);
  439. #endif /* MSDOS */
  440. #else
  441.             (void)UNLINK(str->str_ptr);
  442.             if (link(oldname,str->str_ptr) < 0) {
  443.             warn("Can't rename %s to %s: %s, skipping file",
  444.               oldname, str->str_ptr, strerror(errno) );
  445.             do_close(stab,FALSE);
  446.             str_free(str);
  447.             continue;
  448.             }
  449.             (void)UNLINK(oldname);
  450. #endif
  451.         }
  452.         else {
  453. #ifndef DOSISH
  454.             if (UNLINK(oldname) < 0) {
  455.             warn("Can't rename %s to %s: %s, skipping file",
  456.               oldname, str->str_ptr, strerror(errno) );
  457.             do_close(stab,FALSE);
  458.             str_free(str);
  459.             continue;
  460.             }
  461. #else
  462.             fatal("Can't do inplace edit without backup");
  463. #endif
  464.         }
  465.  
  466.         str_nset(str,">",1);
  467.         str_cat(str,oldname);
  468.         errno = 0;        /* in case sprintf set errno */
  469.         if (!do_open(argvoutstab,str->str_ptr,str->str_cur)) {
  470.             warn("Can't do inplace edit on %s: %s",
  471.               oldname, strerror(errno) );
  472.             do_close(stab,FALSE);
  473.             str_free(str);
  474.             continue;
  475.         }
  476.         defoutstab = argvoutstab;
  477.         lastfd = fileno(stab_io(argvoutstab)->ifp);
  478.         (void)fstat(lastfd,&statbuf);
  479. #ifdef HAS_FCHMOD
  480.         (void)fchmod(lastfd,filemode);
  481. #else
  482.         (void)chmod(oldname,filemode);
  483. #endif
  484.         if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
  485. #ifdef HAS_FCHOWN
  486.             (void)fchown(lastfd,fileuid,filegid);
  487. #else
  488. #ifdef HAS_CHOWN
  489.             (void)chown(oldname,fileuid,filegid);
  490. #endif
  491. #endif
  492.         }
  493.         }
  494.         str_free(str);
  495.         return stab_io(stab)->ifp;
  496.     }
  497.     else
  498.         fprintf(stderr,"Can't open %s: %s\n",str_get(str), strerror(errno));
  499.     str_free(str);
  500.     }
  501.     if (inplace) {
  502.     (void)do_close(argvoutstab,FALSE);
  503.     defoutstab = stabent("STDOUT",TRUE);
  504.     }
  505.     return Nullfp;
  506. }
  507.  
  508. #ifdef HAS_PIPE
  509. void
  510. do_pipe(str, rstab, wstab)
  511. STR *str;
  512. STAB *rstab;
  513. STAB *wstab;
  514. {
  515.     register STIO *rstio;
  516.     register STIO *wstio;
  517.     int fd[2];
  518.  
  519.     if (!rstab)
  520.     goto badexit;
  521.     if (!wstab)
  522.     goto badexit;
  523.  
  524.     rstio = stab_io(rstab);
  525.     wstio = stab_io(wstab);
  526.  
  527.     if (!rstio)
  528.     rstio = stab_io(rstab) = stio_new();
  529.     else if (rstio->ifp)
  530.     do_close(rstab,FALSE);
  531.     if (!wstio)
  532.     wstio = stab_io(wstab) = stio_new();
  533.     else if (wstio->ifp)
  534.     do_close(wstab,FALSE);
  535.  
  536.     if (pipe(fd) < 0)
  537.     goto badexit;
  538.     rstio->ifp = fdopen(fd[0], "r");
  539.     wstio->ofp = fdopen(fd[1], "w");
  540.     wstio->ifp = wstio->ofp;
  541.     rstio->type = '<';
  542.     wstio->type = '>';
  543.     if (!rstio->ifp || !wstio->ofp) {
  544.     if (rstio->ifp) fclose(rstio->ifp);
  545.     else close(fd[0]);
  546.     if (wstio->ofp) fclose(wstio->ofp);
  547.     else close(fd[1]);
  548.     goto badexit;
  549.     }
  550.  
  551.     str_sset(str,&str_yes);
  552.     return;
  553.  
  554. badexit:
  555.     str_sset(str,&str_undef);
  556.     return;
  557. }
  558. #endif
  559.  
  560. bool
  561. do_close(stab,explicit)
  562. STAB *stab;
  563. bool explicit;
  564. {
  565.     bool retval = FALSE;
  566.     register STIO *stio;
  567.     int status;
  568.  
  569.     if (!stab)
  570.     stab = argvstab;
  571.     if (!stab) {
  572.     errno = EBADF;
  573.     return FALSE;
  574.     }
  575.     stio = stab_io(stab);
  576.     if (!stio) {        /* never opened */
  577.     if (dowarn && explicit)
  578.         warn("Close on unopened file <%s>",stab_ename(stab));
  579.     return FALSE;
  580.     }
  581.     if (stio->ifp) {
  582.     if (stio->type == '|') {
  583.         status = mypclose(stio->ifp);
  584.         retval = (status == 0);
  585.         statusvalue = (unsigned short)status & 0xffff;
  586.     }
  587.     else if (stio->type == '-')
  588.         retval = TRUE;
  589.     else {
  590.         if (stio->ofp && stio->ofp != stio->ifp) {        /* a socket */
  591.         retval = (fclose(stio->ofp) != EOF);
  592.         fclose(stio->ifp);    /* clear stdio, fd already closed */
  593.         }
  594.         else
  595.         retval = (fclose(stio->ifp) != EOF);
  596.     }
  597.     stio->ofp = stio->ifp = Nullfp;
  598.     }
  599.     if (explicit)
  600.     stio->lines = 0;
  601.     stio->type = ' ';
  602.     return retval;
  603. }
  604.  
  605. bool
  606. do_eof(stab)
  607. STAB *stab;
  608. {
  609.     register STIO *stio;
  610.     int ch;
  611.  
  612.     if (!stab) {            /* eof() */
  613.     if (argvstab)
  614.         stio = stab_io(argvstab);
  615.     else
  616.         return TRUE;
  617.     }
  618.     else
  619.     stio = stab_io(stab);
  620.  
  621.     if (!stio)
  622.     return TRUE;
  623.  
  624.     while (stio->ifp) {
  625.  
  626. #ifdef STDSTDIO            /* (the code works without this) */
  627.     if (stio->ifp->_cnt > 0)    /* cheat a little, since */
  628.         return FALSE;        /* this is the most usual case */
  629. #endif
  630.  
  631.     ch = getc(stio->ifp);
  632.     if (ch != EOF) {
  633.         (void)ungetc(ch, stio->ifp);
  634.         return FALSE;
  635.     }
  636. #ifdef STDSTDIO
  637.     if (stio->ifp->_cnt < -1)
  638.         stio->ifp->_cnt = -1;
  639. #endif
  640.     if (!stab) {            /* not necessarily a real EOF yet? */
  641.         if (!nextargv(argvstab))    /* get another fp handy */
  642.         return TRUE;
  643.     }
  644.     else
  645.         return TRUE;        /* normal fp, definitely end of file */
  646.     }
  647.     return TRUE;
  648. }
  649.  
  650. long
  651. do_tell(stab)
  652. STAB *stab;
  653. {
  654.     register STIO *stio;
  655.  
  656.     if (!stab)
  657.     goto phooey;
  658.  
  659.     stio = stab_io(stab);
  660.     if (!stio || !stio->ifp)
  661.     goto phooey;
  662.  
  663. #ifdef ULTRIX_STDIO_BOTCH
  664.     if (feof(stio->ifp))
  665.     (void)fseek (stio->ifp, 0L, 2);        /* ultrix 1.2 workaround */
  666. #endif
  667.  
  668.     return ftell(stio->ifp);
  669.  
  670. phooey:
  671.     if (dowarn)
  672.     warn("tell() on unopened file");
  673.     errno = EBADF;
  674.     return -1L;
  675. }
  676.  
  677. bool
  678. do_seek(stab, pos, whence)
  679. STAB *stab;
  680. long pos;
  681. int whence;
  682. {
  683.     register STIO *stio;
  684.  
  685.     if (!stab)
  686.     goto nuts;
  687.  
  688.     stio = stab_io(stab);
  689.     if (!stio || !stio->ifp)
  690.     goto nuts;
  691.  
  692. #ifdef ULTRIX_STDIO_BOTCH
  693.     if (feof(stio->ifp))
  694.     (void)fseek (stio->ifp, 0L, 2);        /* ultrix 1.2 workaround */
  695. #endif
  696.  
  697.     return fseek(stio->ifp, pos, whence) >= 0;
  698.  
  699. nuts:
  700.     if (dowarn)
  701.     warn("seek() on unopened file");
  702.     errno = EBADF;
  703.     return FALSE;
  704. }
  705.  
  706. int
  707. do_ctl(optype,stab,func,argstr)
  708. int optype;
  709. STAB *stab;
  710. int func;
  711. STR *argstr;
  712. {
  713.     register STIO *stio;
  714.     register char *s;
  715.     int retval;
  716.  
  717.     if (!stab || !argstr || !(stio = stab_io(stab)) || !stio->ifp) {
  718.     errno = EBADF;    /* well, sort of... */
  719.     return -1;
  720.     }
  721.  
  722.     if (argstr->str_pok || !argstr->str_nok) {
  723.     if (!argstr->str_pok)
  724.         s = str_get(argstr);
  725.  
  726. #ifdef IOCPARM_MASK
  727. #ifndef IOCPARM_LEN
  728. #define IOCPARM_LEN(x)  (((x) >> 16) & IOCPARM_MASK)
  729. #endif
  730. #endif
  731. #ifdef IOCPARM_LEN
  732.     retval = IOCPARM_LEN(func);    /* on BSDish systes we're safe */
  733. #else
  734.     retval = 256;            /* otherwise guess at what's safe */
  735. #endif
  736.     if (argstr->str_cur < retval) {
  737.         Str_Grow(argstr,retval+1);
  738.         argstr->str_cur = retval;
  739.     }
  740.  
  741.     s = argstr->str_ptr;
  742.     s[argstr->str_cur] = 17;    /* a little sanity check here */
  743.     }
  744.     else {
  745.     retval = (int)str_gnum(argstr);
  746. #ifdef DOSISH
  747.     s = (char*)(long)retval;        /* ouch */
  748. #else
  749.     s = (char*)retval;        /* ouch */
  750. #endif
  751.     }
  752.  
  753. #ifndef lint
  754.     if (optype == O_IOCTL)
  755.     retval = ioctl(fileno(stio->ifp), func, s);
  756.     else
  757. #ifdef DOSISH
  758.     fatal("fcntl is not implemented");
  759. #else
  760. #ifdef HAS_FCNTL
  761.     retval = fcntl(fileno(stio->ifp), func, s);
  762. #else
  763.     fatal("fcntl is not implemented");
  764. #endif
  765. #endif
  766. #else /* lint */
  767.     retval = 0;
  768. #endif /* lint */
  769.  
  770.     if (argstr->str_pok) {
  771.     if (s[argstr->str_cur] != 17)
  772.         fatal("Return value overflowed string");
  773.     s[argstr->str_cur] = 0;        /* put our null back */
  774.     }
  775.     return retval;
  776. }
  777.  
  778. int
  779. do_stat(str,arg,gimme,arglast)
  780. STR *str;
  781. register ARG *arg;
  782. int gimme;
  783. int *arglast;
  784. {
  785.     register ARRAY *ary = stack;
  786.     register int sp = arglast[0] + 1;
  787.     int max = 13;
  788.  
  789.     if ((arg[1].arg_type & A_MASK) == A_WORD) {
  790.     tmpstab = arg[1].arg_ptr.arg_stab;
  791.     if (tmpstab != defstab) {
  792.         laststype = O_STAT;
  793.         statstab = tmpstab;
  794.         str_set(statname,"");
  795.         if (!stab_io(tmpstab) || !stab_io(tmpstab)->ifp ||
  796.           fstat(fileno(stab_io(tmpstab)->ifp),&statcache) < 0) {
  797.         max = 0;
  798.         laststatval = -1;
  799.         }
  800.     }
  801.     else if (laststatval < 0)
  802.         max = 0;
  803.     }
  804.     else {
  805.     str_set(statname,str_get(ary->ary_array[sp]));
  806.     statstab = Nullstab;
  807. #ifdef HAS_LSTAT
  808.     laststype = arg->arg_type;
  809.     if (arg->arg_type == O_LSTAT)
  810.         laststatval = lstat(str_get(statname),&statcache);
  811.     else
  812. #endif
  813.         laststatval = stat(str_get(statname),&statcache);
  814.     if (laststatval < 0) {
  815.         if (dowarn && index(str_get(statname), '\n'))
  816.         warn(warn_nl, "stat");
  817.         max = 0;
  818.     }
  819.     }
  820.  
  821.     if (gimme != G_ARRAY) {
  822.     if (max)
  823.         str_sset(str,&str_yes);
  824.     else
  825.         str_sset(str,&str_undef);
  826.     STABSET(str);
  827.     ary->ary_array[sp] = str;
  828.     return sp;
  829.     }
  830.     sp--;
  831.     if (max) {
  832. #ifndef lint
  833.     (void)astore(ary,++sp,
  834.       str_2mortal(str_nmake((double)statcache.st_dev)));
  835.     (void)astore(ary,++sp,
  836.       str_2mortal(str_nmake((double)statcache.st_ino)));
  837.     (void)astore(ary,++sp,
  838.       str_2mortal(str_nmake((double)statcache.st_mode)));
  839.     (void)astore(ary,++sp,
  840.       str_2mortal(str_nmake((double)statcache.st_nlink)));
  841.     (void)astore(ary,++sp,
  842.       str_2mortal(str_nmake((double)statcache.st_uid)));
  843.     (void)astore(ary,++sp,
  844.       str_2mortal(str_nmake((double)statcache.st_gid)));
  845.     (void)astore(ary,++sp,
  846.       str_2mortal(str_nmake((double)statcache.st_rdev)));
  847.     (void)astore(ary,++sp,
  848.       str_2mortal(str_nmake((double)statcache.st_size)));
  849.     (void)astore(ary,++sp,
  850.       str_2mortal(str_nmake((double)statcache.st_atime)));
  851.     (void)astore(ary,++sp,
  852.       str_2mortal(str_nmake((double)statcache.st_mtime)));
  853.     (void)astore(ary,++sp,
  854.       str_2mortal(str_nmake((double)statcache.st_ctime)));
  855. #ifdef STATBLOCKS
  856.     (void)astore(ary,++sp,
  857.       str_2mortal(str_nmake((double)statcache.st_blksize)));
  858.     (void)astore(ary,++sp,
  859.       str_2mortal(str_nmake((double)statcache.st_blocks)));
  860. #else
  861.     (void)astore(ary,++sp,
  862.       str_2mortal(str_make("",0)));
  863.     (void)astore(ary,++sp,
  864.       str_2mortal(str_make("",0)));
  865. #endif
  866. #else /* lint */
  867.     (void)astore(ary,++sp,str_nmake(0.0));
  868. #endif /* lint */
  869.     }
  870.     return sp;
  871. }
  872.  
  873. #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
  874.     /* code courtesy of William Kucharski */
  875. #define HAS_CHSIZE
  876.  
  877. int chsize(fd, length)
  878. int fd;            /* file descriptor */
  879. off_t length;        /* length to set file to */
  880. {
  881.     extern long lseek();
  882.     struct flock fl;
  883.     struct stat filebuf;
  884.  
  885.     if (fstat(fd, &filebuf) < 0)
  886.     return -1;
  887.  
  888.     if (filebuf.st_size < length) {
  889.  
  890.     /* extend file length */
  891.  
  892.     if ((lseek(fd, (length - 1), 0)) < 0)
  893.         return -1;
  894.  
  895.     /* write a "0" byte */
  896.  
  897.     if ((write(fd, "", 1)) != 1)
  898.         return -1;
  899.     }
  900.     else {
  901.     /* truncate length */
  902.  
  903.     fl.l_whence = 0;
  904.     fl.l_len = 0;
  905.     fl.l_start = length;
  906.     fl.l_type = F_WRLCK;    /* write lock on file space */
  907.  
  908.     /*
  909.     * This relies on the UNDOCUMENTED F_FREESP argument to
  910.     * fcntl(2), which truncates the file so that it ends at the
  911.     * position indicated by fl.l_start.
  912.     *
  913.     * Will minor miracles never cease?
  914.     */
  915.  
  916.     if (fcntl(fd, F_FREESP, &fl) < 0)
  917.         return -1;
  918.  
  919.     }
  920.  
  921.     return 0;
  922. }
  923. #endif /* F_FREESP */
  924.  
  925. int                    /*SUPPRESS 590*/
  926. do_truncate(str,arg,gimme,arglast)
  927. STR *str;
  928. register ARG *arg;
  929. int gimme;
  930. int *arglast;
  931. {
  932.     register ARRAY *ary = stack;
  933.     register int sp = arglast[0] + 1;
  934.     off_t len = (off_t)str_gnum(ary->ary_array[sp+1]);
  935.     int result = 1;
  936.     STAB *tmpstab;
  937.  
  938. #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE)
  939. #ifdef HAS_TRUNCATE
  940.     if ((arg[1].arg_type & A_MASK) == A_WORD) {
  941.     tmpstab = arg[1].arg_ptr.arg_stab;
  942.     if (!stab_io(tmpstab) || !stab_io(tmpstab)->ifp ||
  943.       ftruncate(fileno(stab_io(tmpstab)->ifp), len) < 0)
  944.         result = 0;
  945.     }
  946.     else if (truncate(str_get(ary->ary_array[sp]), len) < 0)
  947.     result = 0;
  948. #else
  949.     if ((arg[1].arg_type & A_MASK) == A_WORD) {
  950.     tmpstab = arg[1].arg_ptr.arg_stab;
  951.     if (!stab_io(tmpstab) || !stab_io(tmpstab)->ifp ||
  952.       chsize(fileno(stab_io(tmpstab)->ifp), len) < 0)
  953.         result = 0;
  954.     }
  955.     else {
  956.     int tmpfd;
  957.  
  958.     if ((tmpfd = open(str_get(ary->ary_array[sp]), 0)) < 0)
  959.         result = 0;
  960.     else {
  961.         if (chsize(tmpfd, len) < 0)
  962.         result = 0;
  963.         close(tmpfd);
  964.     }
  965.     }
  966. #endif
  967.  
  968.     if (result)
  969.     str_sset(str,&str_yes);
  970.     else
  971.     str_sset(str,&str_undef);
  972.     STABSET(str);
  973.     ary->ary_array[sp] = str;
  974.     return sp;
  975. #else
  976.     fatal("truncate not implemented");
  977. #endif
  978. }
  979.  
  980. int
  981. looks_like_number(str)
  982. STR *str;
  983. {
  984.     register char *s;
  985.     register char *send;
  986.  
  987.     if (!str->str_pok)
  988.     return TRUE;
  989.     s = str->str_ptr; 
  990.     send = s + str->str_cur;
  991.     while (isSPACE(*s))
  992.     s++;
  993.     if (s >= send)
  994.     return FALSE;
  995.     if (*s == '+' || *s == '-')
  996.     s++;
  997.     while (isDIGIT(*s))
  998.     s++;
  999.     if (s == send)
  1000.     return TRUE;
  1001.     if (*s == '.') 
  1002.     s++;
  1003.     else if (s == str->str_ptr)
  1004.     return FALSE;
  1005.     while (isDIGIT(*s))
  1006.     s++;
  1007.     if (s == send)
  1008.     return TRUE;
  1009.     if (*s == 'e' || *s == 'E') {
  1010.     s++;
  1011.     if (*s == '+' || *s == '-')
  1012.         s++;
  1013.     while (isDIGIT(*s))
  1014.         s++;
  1015.     }
  1016.     while (isSPACE(*s))
  1017.     s++;
  1018.     if (s >= send)
  1019.     return TRUE;
  1020.     return FALSE;
  1021. }
  1022.  
  1023. bool
  1024. do_print(str,fp)
  1025. register STR *str;
  1026. FILE *fp;
  1027. {
  1028.     register char *tmps;
  1029.  
  1030.     if (!fp) {
  1031.     if (dowarn)
  1032.         warn("print to unopened file");
  1033.     errno = EBADF;
  1034.     return FALSE;
  1035.     }
  1036.     if (!str)
  1037.     return TRUE;
  1038.     if (ofmt &&
  1039.       ((str->str_nok && str->str_u.str_nval != 0.0)
  1040.        || (looks_like_number(str) && str_gnum(str) != 0.0) ) ) {
  1041.     fprintf(fp, ofmt, str->str_u.str_nval);
  1042.     return !ferror(fp);
  1043.     }
  1044.     else {
  1045.     tmps = str_get(str);
  1046.     if (*tmps == 'S' && tmps[1] == 't' && tmps[2] == 'B' && tmps[3] == '\0'
  1047.       && str->str_cur == sizeof(STBP) && strlen(tmps) < str->str_cur) {
  1048.         STR *tmpstr = str_mortal(&str_undef);
  1049.         stab_efullname(tmpstr,((STAB*)str));/* a stab value, be nice */
  1050.         str = tmpstr;
  1051.         tmps = str->str_ptr;
  1052.         putc('*',fp);
  1053.     }
  1054.     if (str->str_cur && (fwrite(tmps,1,str->str_cur,fp) == 0 || ferror(fp)))
  1055.         return FALSE;
  1056.     }
  1057.     return TRUE;
  1058. }
  1059.  
  1060. bool
  1061. do_aprint(arg,fp,arglast)
  1062. register ARG *arg;
  1063. register FILE *fp;
  1064. int *arglast;
  1065. {
  1066.     register STR **st = stack->ary_array;
  1067.     register int sp = arglast[1];
  1068.     register int retval;
  1069.     register int items = arglast[2] - sp;
  1070.  
  1071.     if (!fp) {
  1072.     if (dowarn)
  1073.         warn("print to unopened file");
  1074.     errno = EBADF;
  1075.     return FALSE;
  1076.     }
  1077.     st += ++sp;
  1078.     if (arg->arg_type == O_PRTF) {
  1079.     do_sprintf(arg->arg_ptr.arg_str,items,st);
  1080.     retval = do_print(arg->arg_ptr.arg_str,fp);
  1081.     }
  1082.     else {
  1083.     retval = (items <= 0);
  1084.     for (; items > 0; items--,st++) {
  1085.         if (retval && ofslen) {
  1086.         if (fwrite(ofs, 1, ofslen, fp) == 0 || ferror(fp)) {
  1087.             retval = FALSE;
  1088.             break;
  1089.         }
  1090.         }
  1091.         if (!(retval = do_print(*st, fp)))
  1092.         break;
  1093.     }
  1094.     if (retval && orslen)
  1095.         if (fwrite(ors, 1, orslen, fp) == 0 || ferror(fp))
  1096.         retval = FALSE;
  1097.     }
  1098.     return retval;
  1099. }
  1100.  
  1101. int
  1102. mystat(arg,str)
  1103. ARG *arg;
  1104. STR *str;
  1105. {
  1106.     STIO *stio;
  1107.  
  1108.     if (arg[1].arg_type & A_DONT) {
  1109.     stio = stab_io(arg[1].arg_ptr.arg_stab);
  1110.     if (stio && stio->ifp) {
  1111.         statstab = arg[1].arg_ptr.arg_stab;
  1112.         str_set(statname,"");
  1113.         laststype = O_STAT;
  1114.         return (laststatval = fstat(fileno(stio->ifp), &statcache));
  1115.     }
  1116.     else {
  1117.         if (arg[1].arg_ptr.arg_stab == defstab)
  1118.         return laststatval;
  1119.         if (dowarn)
  1120.         warn("Stat on unopened file <%s>",
  1121.           stab_ename(arg[1].arg_ptr.arg_stab));
  1122.         statstab = Nullstab;
  1123.         str_set(statname,"");
  1124.         return (laststatval = -1);
  1125.     }
  1126.     }
  1127.     else {
  1128.     statstab = Nullstab;
  1129.     str_set(statname,str_get(str));
  1130.     laststype = O_STAT;
  1131.     laststatval = stat(str_get(str),&statcache);
  1132.     if (laststatval < 0 && dowarn && index(str_get(str), '\n'))
  1133.         warn(warn_nl, "stat");
  1134.     return laststatval;
  1135.     }
  1136. }
  1137.  
  1138. int
  1139. mylstat(arg,str)
  1140. ARG *arg;
  1141. STR *str;
  1142. {
  1143.     if (arg[1].arg_type & A_DONT) {
  1144.     if (arg[1].arg_ptr.arg_stab == defstab) {
  1145.         if (laststype != O_LSTAT)
  1146.         fatal("The stat preceding -l _ wasn't an lstat");
  1147.         return laststatval;
  1148.     }
  1149.     fatal("You can't use -l on a filehandle");
  1150.     }
  1151.  
  1152.     laststype = O_LSTAT;
  1153.     statstab = Nullstab;
  1154.     str_set(statname,str_get(str));
  1155. #ifdef HAS_LSTAT
  1156.     laststatval = lstat(str_get(str),&statcache);
  1157. #else
  1158.     laststatval = stat(str_get(str),&statcache);
  1159. #endif
  1160.     if (laststatval < 0 && dowarn && index(str_get(str), '\n'))
  1161.     warn(warn_nl, "lstat");
  1162.     return laststatval;
  1163. }
  1164.  
  1165. STR *
  1166. do_fttext(arg,str)
  1167. register ARG *arg;
  1168. STR *str;
  1169. {
  1170.     int i;
  1171.     int len;
  1172.     int odd = 0;
  1173.     STDCHAR tbuf[512];
  1174.     register STDCHAR *s;
  1175.     register STIO *stio;
  1176.  
  1177.     if (arg[1].arg_type & A_DONT) {
  1178.     if (arg[1].arg_ptr.arg_stab == defstab) {
  1179.         if (statstab)
  1180.         stio = stab_io(statstab);
  1181.         else {
  1182.         str = statname;
  1183.         goto really_filename;
  1184.         }
  1185.     }
  1186.     else {
  1187.         statstab = arg[1].arg_ptr.arg_stab;
  1188.         str_set(statname,"");
  1189.         stio = stab_io(statstab);
  1190.     }
  1191.     if (stio && stio->ifp) {
  1192. #if defined(STDSTDIO) || defined(atarist) /* this will work with atariST */
  1193.         fstat(fileno(stio->ifp),&statcache);
  1194.         if (S_ISDIR(statcache.st_mode))    /* handle NFS glitch */
  1195.         return arg->arg_type == O_FTTEXT ? &str_no : &str_yes;
  1196.         if (stio->ifp->_cnt <= 0) {
  1197.         i = getc(stio->ifp);
  1198.         if (i != EOF)
  1199.             (void)ungetc(i,stio->ifp);
  1200.         }
  1201.         if (stio->ifp->_cnt <= 0)    /* null file is anything */
  1202.         return &str_yes;
  1203.         len = stio->ifp->_cnt + (stio->ifp->_ptr - stio->ifp->_base);
  1204.         s = stio->ifp->_base;
  1205. #else
  1206.         fatal("-T and -B not implemented on filehandles");
  1207. #endif
  1208.     }
  1209.     else {
  1210.         if (dowarn)
  1211.         warn("Test on unopened file <%s>",
  1212.           stab_ename(arg[1].arg_ptr.arg_stab));
  1213.         errno = EBADF;
  1214.         return &str_undef;
  1215.     }
  1216.     }
  1217.     else {
  1218.     statstab = Nullstab;
  1219.     str_set(statname,str_get(str));
  1220.       really_filename:
  1221.     i = open(str_get(str),0);
  1222.     if (i < 0) {
  1223.         if (dowarn && index(str_get(str), '\n'))
  1224.         warn(warn_nl, "open");
  1225.         return &str_undef;
  1226.     }
  1227.     fstat(i,&statcache);
  1228.     len = read(i,tbuf,512);
  1229.     (void)close(i);
  1230.     if (len <= 0) {
  1231.         if (S_ISDIR(statcache.st_mode) && arg->arg_type == O_FTTEXT)
  1232.         return &str_no;        /* special case NFS directories */
  1233.         return &str_yes;        /* null file is anything */
  1234.     }
  1235.     s = tbuf;
  1236.     }
  1237.  
  1238.     /* now scan s to look for textiness */
  1239.  
  1240.     for (i = 0; i < len; i++,s++) {
  1241.     if (!*s) {            /* null never allowed in text */
  1242.         odd += len;
  1243.         break;
  1244.     }
  1245.     else if (*s & 128)
  1246.         odd++;
  1247.     else if (*s < 32 &&
  1248.       *s != '\n' && *s != '\r' && *s != '\b' &&
  1249.       *s != '\t' && *s != '\f' && *s != 27)
  1250.         odd++;
  1251.     }
  1252.  
  1253.     if ((odd * 10 > len) == (arg->arg_type == O_FTTEXT)) /* allow 10% odd */
  1254.     return &str_no;
  1255.     else
  1256.     return &str_yes;
  1257. }
  1258.  
  1259. static char **Argv = Null(char **);
  1260. static char *Cmd = Nullch;
  1261.  
  1262. bool
  1263. do_aexec(really,arglast)
  1264. STR *really;
  1265. int *arglast;
  1266. {
  1267.     register STR **st = stack->ary_array;
  1268.     register int sp = arglast[1];
  1269.     register int items = arglast[2] - sp;
  1270.     register char **a;
  1271.     char *tmps;
  1272.  
  1273.     if (items) {
  1274.     New(401,Argv, items+1, char*);
  1275.     a = Argv;
  1276.     for (st += ++sp; items > 0; items--,st++) {
  1277.         if (*st)
  1278.         *a++ = str_get(*st);
  1279.         else
  1280.         *a++ = "";
  1281.     }
  1282.     *a = Nullch;
  1283. #ifdef TAINT
  1284.     if (*Argv[0] != '/')    /* will execvp use PATH? */
  1285.         taintenv();        /* testing IFS here is overkill, probably */
  1286. #endif
  1287.     if (really && *(tmps = str_get(really))) {
  1288.         execvp(tmps,Argv);
  1289.     }
  1290.     else {
  1291.         execvp(Argv[0],Argv);
  1292.     }
  1293.     }
  1294.     do_execfree();
  1295.     return FALSE;
  1296. }
  1297.  
  1298. void
  1299. do_execfree()
  1300. {
  1301.     if (Argv) {
  1302.     Safefree(Argv);
  1303.     Argv = Null(char **);
  1304.     }
  1305.     if (Cmd) {
  1306.     Safefree(Cmd);
  1307.     Cmd = Nullch;
  1308.     }
  1309. }
  1310.  
  1311. bool
  1312. do_exec(cmd)
  1313. char *cmd;
  1314. {
  1315.     register char **a;
  1316.     register char *s;
  1317.     char flags[10];
  1318.  
  1319.     /* save an extra exec if possible */
  1320.  
  1321. #ifdef CSH
  1322.     if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
  1323.     strcpy(flags,"-c");
  1324.     s = cmd+cshlen+3;
  1325.     if (*s == 'f') {
  1326.         s++;
  1327.         strcat(flags,"f");
  1328.     }
  1329.     if (*s == ' ')
  1330.         s++;
  1331.     if (*s++ == '\'') {
  1332.         char *ncmd = s;
  1333.  
  1334.         while (*s)
  1335.         s++;
  1336.         if (s[-1] == '\n')
  1337.         *--s = '\0';
  1338.         if (s[-1] == '\'') {
  1339.         *--s = '\0';
  1340.         execl(cshname,"csh", flags,ncmd,(char*)0);
  1341.         *s = '\'';
  1342.         return FALSE;
  1343.         }
  1344.     }
  1345.     }
  1346. #endif /* CSH */
  1347.  
  1348.     /* see if there are shell metacharacters in it */
  1349.  
  1350.     /*SUPPRESS 530*/
  1351.     for (s = cmd; *s && isALPHA(*s); s++) ;    /* catch VAR=val gizmo */
  1352.     if (*s == '=')
  1353.     goto doshell;
  1354.     for (s = cmd; *s; s++) {
  1355.     if (*s != ' ' && !isALPHA(*s) && index("$&*(){}[]'\";\\|?<>~`\n",*s)) {
  1356.         if (*s == '\n' && !s[1]) {
  1357.         *s = '\0';
  1358.         break;
  1359.         }
  1360.       doshell:
  1361.         execl("/bin/sh","sh","-c",cmd,(char*)0);
  1362.         return FALSE;
  1363.     }
  1364.     }
  1365.     New(402,Argv, (s - cmd) / 2 + 2, char*);
  1366.     Cmd = nsavestr(cmd, s-cmd);
  1367.     a = Argv;
  1368.     for (s = Cmd; *s;) {
  1369.     while (*s && isSPACE(*s)) s++;
  1370.     if (*s)
  1371.         *(a++) = s;
  1372.     while (*s && !isSPACE(*s)) s++;
  1373.     if (*s)
  1374.         *s++ = '\0';
  1375.     }
  1376.     *a = Nullch;
  1377.     if (Argv[0]) {
  1378.     execvp(Argv[0],Argv);
  1379.     if (errno == ENOEXEC) {        /* for system V NIH syndrome */
  1380.         do_execfree();
  1381.         goto doshell;
  1382.     }
  1383.     }
  1384.     do_execfree();
  1385.     return FALSE;
  1386. }
  1387.  
  1388. #ifdef HAS_SOCKET
  1389. int
  1390. do_socket(stab, arglast)
  1391. STAB *stab;
  1392. int *arglast;
  1393. {
  1394.     register STR **st = stack->ary_array;
  1395.     register int sp = arglast[1];
  1396.     register STIO *stio;
  1397.     int domain, type, protocol, fd;
  1398.  
  1399.     if (!stab) {
  1400.     errno = EBADF;
  1401.     return FALSE;
  1402.     }
  1403.  
  1404.     stio = stab_io(stab);
  1405.     if (!stio)
  1406.     stio = stab_io(stab) = stio_new();
  1407.     else if (stio->ifp)
  1408.     do_close(stab,FALSE);
  1409.  
  1410.     domain = (int)str_gnum(st[++sp]);
  1411.     type = (int)str_gnum(st[++sp]);
  1412.     protocol = (int)str_gnum(st[++sp]);
  1413. #ifdef TAINT
  1414.     taintproper("Insecure dependency in socket");
  1415. #endif
  1416.     fd = socket(domain,type,protocol);
  1417.     if (fd < 0)
  1418.     return FALSE;
  1419.     stio->ifp = fdopen(fd, "r");    /* stdio gets confused about sockets */
  1420.     stio->ofp = fdopen(fd, "w");
  1421.     stio->type = 's';
  1422.     if (!stio->ifp || !stio->ofp) {
  1423.     if (stio->ifp) fclose(stio->ifp);
  1424.     if (stio->ofp) fclose(stio->ofp);
  1425.     if (!stio->ifp && !stio->ofp) close(fd);
  1426.     return FALSE;
  1427.     }
  1428.  
  1429.     return TRUE;
  1430. }
  1431.  
  1432. int
  1433. do_bind(stab, arglast)
  1434. STAB *stab;
  1435. int *arglast;
  1436. {
  1437.     register STR **st = stack->ary_array;
  1438.     register int sp = arglast[1];
  1439.     register STIO *stio;
  1440.     char *addr;
  1441.  
  1442.     if (!stab)
  1443.     goto nuts;
  1444.  
  1445.     stio = stab_io(stab);
  1446.     if (!stio || !stio->ifp)
  1447.     goto nuts;
  1448.  
  1449.     addr = str_get(st[++sp]);
  1450. #ifdef TAINT
  1451.     taintproper("Insecure dependency in bind");
  1452. #endif
  1453.     return bind(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
  1454.  
  1455. nuts:
  1456.     if (dowarn)
  1457.     warn("bind() on closed fd");
  1458.     errno = EBADF;
  1459.     return FALSE;
  1460.  
  1461. }
  1462.  
  1463. int
  1464. do_connect(stab, arglast)
  1465. STAB *stab;
  1466. int *arglast;
  1467. {
  1468.     register STR **st = stack->ary_array;
  1469.     register int sp = arglast[1];
  1470.     register STIO *stio;
  1471.     char *addr;
  1472.  
  1473.     if (!stab)
  1474.     goto nuts;
  1475.  
  1476.     stio = stab_io(stab);
  1477.     if (!stio || !stio->ifp)
  1478.     goto nuts;
  1479.  
  1480.     addr = str_get(st[++sp]);
  1481. #ifdef TAINT
  1482.     taintproper("Insecure dependency in connect");
  1483. #endif
  1484.     return connect(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
  1485.  
  1486. nuts:
  1487.     if (dowarn)
  1488.     warn("connect() on closed fd");
  1489.     errno = EBADF;
  1490.     return FALSE;
  1491.  
  1492. }
  1493.  
  1494. int
  1495. do_listen(stab, arglast)
  1496. STAB *stab;
  1497. int *arglast;
  1498. {
  1499.     register STR **st = stack->ary_array;
  1500.     register int sp = arglast[1];
  1501.     register STIO *stio;
  1502.     int backlog;
  1503.  
  1504.     if (!stab)
  1505.     goto nuts;
  1506.  
  1507.     stio = stab_io(stab);
  1508.     if (!stio || !stio->ifp)
  1509.     goto nuts;
  1510.  
  1511.     backlog = (int)str_gnum(st[++sp]);
  1512.     return listen(fileno(stio->ifp), backlog) >= 0;
  1513.  
  1514. nuts:
  1515.     if (dowarn)
  1516.     warn("listen() on closed fd");
  1517.     errno = EBADF;
  1518.     return FALSE;
  1519. }
  1520.  
  1521. void
  1522. do_accept(str, nstab, gstab)
  1523. STR *str;
  1524. STAB *nstab;
  1525. STAB *gstab;
  1526. {
  1527.     register STIO *nstio;
  1528.     register STIO *gstio;
  1529.     int len = sizeof buf;
  1530.     int fd;
  1531.  
  1532.     if (!nstab)
  1533.     goto badexit;
  1534.     if (!gstab)
  1535.     goto nuts;
  1536.  
  1537.     gstio = stab_io(gstab);
  1538.     nstio = stab_io(nstab);
  1539.  
  1540.     if (!gstio || !gstio->ifp)
  1541.     goto nuts;
  1542.     if (!nstio)
  1543.     nstio = stab_io(nstab) = stio_new();
  1544.     else if (nstio->ifp)
  1545.     do_close(nstab,FALSE);
  1546.  
  1547.     fd = accept(fileno(gstio->ifp),(struct sockaddr *)buf,&len);
  1548.     if (fd < 0)
  1549.     goto badexit;
  1550.     nstio->ifp = fdopen(fd, "r");
  1551.     nstio->ofp = fdopen(fd, "w");
  1552.     nstio->type = 's';
  1553.     if (!nstio->ifp || !nstio->ofp) {
  1554.     if (nstio->ifp) fclose(nstio->ifp);
  1555.     if (nstio->ofp) fclose(nstio->ofp);
  1556.     if (!nstio->ifp && !nstio->ofp) close(fd);
  1557.     goto badexit;
  1558.     }
  1559.  
  1560.     str_nset(str, buf, len);
  1561.     return;
  1562.  
  1563. nuts:
  1564.     if (dowarn)
  1565.     warn("accept() on closed fd");
  1566.     errno = EBADF;
  1567. badexit:
  1568.     str_sset(str,&str_undef);
  1569.     return;
  1570. }
  1571.  
  1572. int
  1573. do_shutdown(stab, arglast)
  1574. STAB *stab;
  1575. int *arglast;
  1576. {
  1577.     register STR **st = stack->ary_array;
  1578.     register int sp = arglast[1];
  1579.     register STIO *stio;
  1580.     int how;
  1581.  
  1582.     if (!stab)
  1583.     goto nuts;
  1584.  
  1585.     stio = stab_io(stab);
  1586.     if (!stio || !stio->ifp)
  1587.     goto nuts;
  1588.  
  1589.     how = (int)str_gnum(st[++sp]);
  1590.     return shutdown(fileno(stio->ifp), how) >= 0;
  1591.  
  1592. nuts:
  1593.     if (dowarn)
  1594.     warn("shutdown() on closed fd");
  1595.     errno = EBADF;
  1596.     return FALSE;
  1597.  
  1598. }
  1599.  
  1600. int
  1601. do_sopt(optype, stab, arglast)
  1602. int optype;
  1603. STAB *stab;
  1604. int *arglast;
  1605. {
  1606.     register STR **st = stack->ary_array;
  1607.     register int sp = arglast[1];
  1608.     register STIO *stio;
  1609.     int fd;
  1610.     unsigned int lvl;
  1611.     unsigned int optname;
  1612.  
  1613.     if (!stab)
  1614.     goto nuts;
  1615.  
  1616.     stio = stab_io(stab);
  1617.     if (!stio || !stio->ifp)
  1618.     goto nuts;
  1619.  
  1620.     fd = fileno(stio->ifp);
  1621.     lvl = (unsigned int)str_gnum(st[sp+1]);
  1622.     optname = (unsigned int)str_gnum(st[sp+2]);
  1623.     switch (optype) {
  1624.     case O_GSOCKOPT:
  1625.     st[sp] = str_2mortal(Str_new(22,257));
  1626.     st[sp]->str_cur = 256;
  1627.     st[sp]->str_pok = 1;
  1628.     if (getsockopt(fd, lvl, optname, st[sp]->str_ptr,
  1629.             (int*)&st[sp]->str_cur) < 0)
  1630.         goto nuts;
  1631.     break;
  1632.     case O_SSOCKOPT:
  1633.     st[sp] = st[sp+3];
  1634.     if (setsockopt(fd, lvl, optname, st[sp]->str_ptr, st[sp]->str_cur) < 0)
  1635.         goto nuts;
  1636.     st[sp] = &str_yes;
  1637.     break;
  1638.     }
  1639.     
  1640.     return sp;
  1641.  
  1642. nuts:
  1643.     if (dowarn)
  1644.     warn("[gs]etsockopt() on closed fd");
  1645.     st[sp] = &str_undef;
  1646.     errno = EBADF;
  1647.     return sp;
  1648.  
  1649. }
  1650.  
  1651. int
  1652. do_getsockname(optype, stab, arglast)
  1653. int optype;
  1654. STAB *stab;
  1655. int *arglast;
  1656. {
  1657.     register STR **st = stack->ary_array;
  1658.     register int sp = arglast[1];
  1659.     register STIO *stio;
  1660.     int fd;
  1661.  
  1662.     if (!stab)
  1663.     goto nuts;
  1664.  
  1665.     stio = stab_io(stab);
  1666.     if (!stio || !stio->ifp)
  1667.     goto nuts;
  1668.  
  1669.     st[sp] = str_2mortal(Str_new(22,257));
  1670.     st[sp]->str_cur = 256;
  1671.     st[sp]->str_pok = 1;
  1672.     fd = fileno(stio->ifp);
  1673.     switch (optype) {
  1674.     case O_GETSOCKNAME:
  1675.     if (getsockname(fd, st[sp]->str_ptr, (int*)&st[sp]->str_cur) < 0)
  1676.         goto nuts2;
  1677.     break;
  1678.     case O_GETPEERNAME:
  1679.     if (getpeername(fd, st[sp]->str_ptr, (int*)&st[sp]->str_cur) < 0)
  1680.         goto nuts2;
  1681.     break;
  1682.     }
  1683.     
  1684.     return sp;
  1685.  
  1686. nuts:
  1687.     if (dowarn)
  1688.     warn("get{sock,peer}name() on closed fd");
  1689.     errno = EBADF;
  1690. nuts2:
  1691.     st[sp] = &str_undef;
  1692.     return sp;
  1693.  
  1694. }
  1695.  
  1696. int
  1697. do_ghent(which,gimme,arglast)
  1698. int which;
  1699. int gimme;
  1700. int *arglast;
  1701. {
  1702.     register ARRAY *ary = stack;
  1703.     register int sp = arglast[0];
  1704.     register char **elem;
  1705.     register STR *str;
  1706.     struct hostent *gethostbyname();
  1707.     struct hostent *gethostbyaddr();
  1708. #ifdef HAS_GETHOSTENT
  1709.     struct hostent *gethostent();
  1710. #endif
  1711.     struct hostent *hent;
  1712.     unsigned long len;
  1713.  
  1714.     if (which == O_GHBYNAME) {
  1715.     char *name = str_get(ary->ary_array[sp+1]);
  1716.  
  1717.     hent = gethostbyname(name);
  1718.     }
  1719.     else if (which == O_GHBYADDR) {
  1720.     STR *addrstr = ary->ary_array[sp+1];
  1721.     int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
  1722.     char *addr = str_get(addrstr);
  1723.  
  1724.     hent = gethostbyaddr(addr,addrstr->str_cur,addrtype);
  1725.     }
  1726.     else
  1727. #ifdef HAS_GETHOSTENT
  1728.     hent = gethostent();
  1729. #else
  1730.     fatal("gethostent not implemented");
  1731. #endif
  1732.  
  1733. #ifdef HOST_NOT_FOUND
  1734.     if (!hent)
  1735.     statusvalue = (unsigned short)h_errno & 0xffff;
  1736. #endif
  1737.  
  1738.     if (gimme != G_ARRAY) {
  1739.     astore(ary, ++sp, str = str_mortal(&str_undef));
  1740.     if (hent) {
  1741.         if (which == O_GHBYNAME) {
  1742. #ifdef h_addr
  1743.         str_nset(str, *hent->h_addr, hent->h_length);
  1744. #else
  1745.         str_nset(str, hent->h_addr, hent->h_length);
  1746. #endif
  1747.         }
  1748.         else
  1749.         str_set(str, hent->h_name);
  1750.     }
  1751.     return sp;
  1752.     }
  1753.  
  1754.     if (hent) {
  1755. #ifndef lint
  1756.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1757.     str_set(str, hent->h_name);
  1758.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1759.     for (elem = hent->h_aliases; *elem; elem++) {
  1760.         str_cat(str, *elem);
  1761.         if (elem[1])
  1762.         str_ncat(str," ",1);
  1763.     }
  1764.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1765.     str_numset(str, (double)hent->h_addrtype);
  1766.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1767.     len = hent->h_length;
  1768.     str_numset(str, (double)len);
  1769. #ifdef h_addr
  1770.     for (elem = hent->h_addr_list; *elem; elem++) {
  1771.         (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1772.         str_nset(str, *elem, len);
  1773.     }
  1774. #else
  1775.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1776.     str_nset(str, hent->h_addr, len);
  1777. #endif /* h_addr */
  1778. #else /* lint */
  1779.     elem = Nullch;
  1780.     elem = elem;
  1781.     (void)astore(ary, ++sp, str_mortal(&str_no));
  1782. #endif /* lint */
  1783.     }
  1784.  
  1785.     return sp;
  1786. }
  1787.  
  1788. int
  1789. do_gnent(which,gimme,arglast)
  1790. int which;
  1791. int gimme;
  1792. int *arglast;
  1793. {
  1794.     register ARRAY *ary = stack;
  1795.     register int sp = arglast[0];
  1796.     register char **elem;
  1797.     register STR *str;
  1798.     struct netent *getnetbyname();
  1799.     struct netent *getnetbyaddr();
  1800.     struct netent *getnetent();
  1801.     struct netent *nent;
  1802.  
  1803.     if (which == O_GNBYNAME) {
  1804.     char *name = str_get(ary->ary_array[sp+1]);
  1805.  
  1806.     nent = getnetbyname(name);
  1807.     }
  1808.     else if (which == O_GNBYADDR) {
  1809.     unsigned long addr = U_L(str_gnum(ary->ary_array[sp+1]));
  1810.     int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
  1811.  
  1812.     nent = getnetbyaddr((long)addr,addrtype);
  1813.     }
  1814.     else
  1815.     nent = getnetent();
  1816.  
  1817.     if (gimme != G_ARRAY) {
  1818.     astore(ary, ++sp, str = str_mortal(&str_undef));
  1819.     if (nent) {
  1820.         if (which == O_GNBYNAME)
  1821.         str_numset(str, (double)nent->n_net);
  1822.         else
  1823.         str_set(str, nent->n_name);
  1824.     }
  1825.     return sp;
  1826.     }
  1827.  
  1828.     if (nent) {
  1829. #ifndef lint
  1830.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1831.     str_set(str, nent->n_name);
  1832.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1833.     for (elem = nent->n_aliases; *elem; elem++) {
  1834.         str_cat(str, *elem);
  1835.         if (elem[1])
  1836.         str_ncat(str," ",1);
  1837.     }
  1838.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1839.     str_numset(str, (double)nent->n_addrtype);
  1840.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1841.     str_numset(str, (double)nent->n_net);
  1842. #else /* lint */
  1843.     elem = Nullch;
  1844.     elem = elem;
  1845.     (void)astore(ary, ++sp, str_mortal(&str_no));
  1846. #endif /* lint */
  1847.     }
  1848.  
  1849.     return sp;
  1850. }
  1851.  
  1852. int
  1853. do_gpent(which,gimme,arglast)
  1854. int which;
  1855. int gimme;
  1856. int *arglast;
  1857. {
  1858.     register ARRAY *ary = stack;
  1859.     register int sp = arglast[0];
  1860.     register char **elem;
  1861.     register STR *str;
  1862.     struct protoent *getprotobyname();
  1863.     struct protoent *getprotobynumber();
  1864.     struct protoent *getprotoent();
  1865.     struct protoent *pent;
  1866.  
  1867.     if (which == O_GPBYNAME) {
  1868.     char *name = str_get(ary->ary_array[sp+1]);
  1869.  
  1870.     pent = getprotobyname(name);
  1871.     }
  1872.     else if (which == O_GPBYNUMBER) {
  1873.     int proto = (int)str_gnum(ary->ary_array[sp+1]);
  1874.  
  1875.     pent = getprotobynumber(proto);
  1876.     }
  1877.     else
  1878.     pent = getprotoent();
  1879.  
  1880.     if (gimme != G_ARRAY) {
  1881.     astore(ary, ++sp, str = str_mortal(&str_undef));
  1882.     if (pent) {
  1883.         if (which == O_GPBYNAME)
  1884.         str_numset(str, (double)pent->p_proto);
  1885.         else
  1886.         str_set(str, pent->p_name);
  1887.     }
  1888.     return sp;
  1889.     }
  1890.  
  1891.     if (pent) {
  1892. #ifndef lint
  1893.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1894.     str_set(str, pent->p_name);
  1895.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1896.     for (elem = pent->p_aliases; *elem; elem++) {
  1897.         str_cat(str, *elem);
  1898.         if (elem[1])
  1899.         str_ncat(str," ",1);
  1900.     }
  1901.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1902.     str_numset(str, (double)pent->p_proto);
  1903. #else /* lint */
  1904.     elem = Nullch;
  1905.     elem = elem;
  1906.     (void)astore(ary, ++sp, str_mortal(&str_no));
  1907. #endif /* lint */
  1908.     }
  1909.  
  1910.     return sp;
  1911. }
  1912.  
  1913. int
  1914. do_gsent(which,gimme,arglast)
  1915. int which;
  1916. int gimme;
  1917. int *arglast;
  1918. {
  1919.     register ARRAY *ary = stack;
  1920.     register int sp = arglast[0];
  1921.     register char **elem;
  1922.     register STR *str;
  1923.     struct servent *getservbyname();
  1924.     struct servent *getservbynumber();
  1925.     struct servent *getservent();
  1926.     struct servent *sent;
  1927.  
  1928.     if (which == O_GSBYNAME) {
  1929.     char *name = str_get(ary->ary_array[sp+1]);
  1930.     char *proto = str_get(ary->ary_array[sp+2]);
  1931.  
  1932.     if (proto && !*proto)
  1933.         proto = Nullch;
  1934.  
  1935.     sent = getservbyname(name,proto);
  1936.     }
  1937.     else if (which == O_GSBYPORT) {
  1938.     int port = (int)str_gnum(ary->ary_array[sp+1]);
  1939.     char *proto = str_get(ary->ary_array[sp+2]);
  1940.  
  1941.     sent = getservbyport(port,proto);
  1942.     }
  1943.     else
  1944.     sent = getservent();
  1945.  
  1946.     if (gimme != G_ARRAY) {
  1947.     astore(ary, ++sp, str = str_mortal(&str_undef));
  1948.     if (sent) {
  1949.         if (which == O_GSBYNAME) {
  1950. #ifdef HAS_NTOHS
  1951.         str_numset(str, (double)ntohs(sent->s_port));
  1952. #else
  1953.         str_numset(str, (double)(sent->s_port));
  1954. #endif
  1955.         }
  1956.         else
  1957.         str_set(str, sent->s_name);
  1958.     }
  1959.     return sp;
  1960.     }
  1961.  
  1962.     if (sent) {
  1963. #ifndef lint
  1964.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1965.     str_set(str, sent->s_name);
  1966.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1967.     for (elem = sent->s_aliases; *elem; elem++) {
  1968.         str_cat(str, *elem);
  1969.         if (elem[1])
  1970.         str_ncat(str," ",1);
  1971.     }
  1972.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1973. #ifdef HAS_NTOHS
  1974.     str_numset(str, (double)ntohs(sent->s_port));
  1975. #else
  1976.     str_numset(str, (double)(sent->s_port));
  1977. #endif
  1978.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  1979.     str_set(str, sent->s_proto);
  1980. #else /* lint */
  1981.     elem = Nullch;
  1982.     elem = elem;
  1983.     (void)astore(ary, ++sp, str_mortal(&str_no));
  1984. #endif /* lint */
  1985.     }
  1986.  
  1987.     return sp;
  1988. }
  1989.  
  1990. #endif /* HAS_SOCKET */
  1991.  
  1992. #ifdef HAS_SELECT
  1993. int
  1994. do_select(gimme,arglast)
  1995. int gimme;
  1996. int *arglast;
  1997. {
  1998.     register STR **st = stack->ary_array;
  1999.     register int sp = arglast[0];
  2000.     register int i;
  2001.     register int j;
  2002.     register char *s;
  2003.     register STR *str;
  2004.     double value;
  2005.     int maxlen = 0;
  2006.     int nfound;
  2007.     struct timeval timebuf;
  2008.     struct timeval *tbuf = &timebuf;
  2009.     int growsize;
  2010. #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
  2011.     int masksize;
  2012.     int offset;
  2013.     char *fd_sets[4];
  2014.     int k;
  2015.  
  2016. #if BYTEORDER & 0xf0000
  2017. #define ORDERBYTE (0x88888888 - BYTEORDER)
  2018. #else
  2019. #define ORDERBYTE (0x4444 - BYTEORDER)
  2020. #endif
  2021.  
  2022. #endif
  2023.  
  2024.     for (i = 1; i <= 3; i++) {
  2025.     j = st[sp+i]->str_cur;
  2026.     if (maxlen < j)
  2027.         maxlen = j;
  2028.     }
  2029.  
  2030. #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
  2031.     growsize = maxlen;        /* little endians can use vecs directly */
  2032. #else
  2033. #ifdef NFDBITS
  2034.  
  2035. #ifndef NBBY
  2036. #define NBBY 8
  2037. #endif
  2038.  
  2039.     masksize = NFDBITS / NBBY;
  2040. #else
  2041.     masksize = sizeof(long);    /* documented int, everyone seems to use long */
  2042. #endif
  2043.     growsize = maxlen + (masksize - (maxlen % masksize));
  2044.     Zero(&fd_sets[0], 4, char*);
  2045. #endif
  2046.  
  2047.     for (i = 1; i <= 3; i++) {
  2048.     str = st[sp+i];
  2049.     j = str->str_len;
  2050.     if (j < growsize) {
  2051.         if (str->str_pok) {
  2052.         Str_Grow(str,growsize);
  2053.         s = str_get(str) + j;
  2054.         while (++j <= growsize) {
  2055.             *s++ = '\0';
  2056.         }
  2057.         }
  2058.         else if (str->str_ptr) {
  2059.         Safefree(str->str_ptr);
  2060.         str->str_ptr = Nullch;
  2061.         }
  2062.     }
  2063. #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
  2064.     s = str->str_ptr;
  2065.     if (s) {
  2066.         New(403, fd_sets[i], growsize, char);
  2067.         for (offset = 0; offset < growsize; offset += masksize) {
  2068.         for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
  2069.             fd_sets[i][j+offset] = s[(k % masksize) + offset];
  2070.         }
  2071.     }
  2072. #endif
  2073.     }
  2074.     str = st[sp+4];
  2075.     if (str->str_nok || str->str_pok) {
  2076.     value = str_gnum(str);
  2077.     if (value < 0.0)
  2078.         value = 0.0;
  2079.     timebuf.tv_sec = (long)value;
  2080.     value -= (double)timebuf.tv_sec;
  2081.     timebuf.tv_usec = (long)(value * 1000000.0);
  2082.     }
  2083.     else
  2084.     tbuf = Null(struct timeval*);
  2085.  
  2086. #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
  2087.     nfound = select(
  2088.     maxlen * 8,
  2089.     st[sp+1]->str_ptr,
  2090.     st[sp+2]->str_ptr,
  2091.     st[sp+3]->str_ptr,
  2092.     tbuf);
  2093. #else
  2094.     nfound = select(
  2095.     maxlen * 8,
  2096.     fd_sets[1],
  2097.     fd_sets[2],
  2098.     fd_sets[3],
  2099.     tbuf);
  2100.     for (i = 1; i <= 3; i++) {
  2101.     if (fd_sets[i]) {
  2102.         str = st[sp+i];
  2103.         s = str->str_ptr;
  2104.         for (offset = 0; offset < growsize; offset += masksize) {
  2105.         for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
  2106.             s[(k % masksize) + offset] = fd_sets[i][j+offset];
  2107.         }
  2108.         Safefree(fd_sets[i]);
  2109.     }
  2110.     }
  2111. #endif
  2112.  
  2113.     st[++sp] = str_mortal(&str_no);
  2114.     str_numset(st[sp], (double)nfound);
  2115.     if (gimme == G_ARRAY && tbuf) {
  2116.     value = (double)(timebuf.tv_sec) +
  2117.         (double)(timebuf.tv_usec) / 1000000.0;
  2118.     st[++sp] = str_mortal(&str_no);
  2119.     str_numset(st[sp], value);
  2120.     }
  2121.     return sp;
  2122. }
  2123. #endif /* SELECT */
  2124.  
  2125. #ifdef HAS_SOCKET
  2126. int
  2127. do_spair(stab1, stab2, arglast)
  2128. STAB *stab1;
  2129. STAB *stab2;
  2130. int *arglast;
  2131. {
  2132.     register STR **st = stack->ary_array;
  2133.     register int sp = arglast[2];
  2134.     register STIO *stio1;
  2135.     register STIO *stio2;
  2136.     int domain, type, protocol, fd[2];
  2137.  
  2138.     if (!stab1 || !stab2)
  2139.     return FALSE;
  2140.  
  2141.     stio1 = stab_io(stab1);
  2142.     stio2 = stab_io(stab2);
  2143.     if (!stio1)
  2144.     stio1 = stab_io(stab1) = stio_new();
  2145.     else if (stio1->ifp)
  2146.     do_close(stab1,FALSE);
  2147.     if (!stio2)
  2148.     stio2 = stab_io(stab2) = stio_new();
  2149.     else if (stio2->ifp)
  2150.     do_close(stab2,FALSE);
  2151.  
  2152.     domain = (int)str_gnum(st[++sp]);
  2153.     type = (int)str_gnum(st[++sp]);
  2154.     protocol = (int)str_gnum(st[++sp]);
  2155. #ifdef TAINT
  2156.     taintproper("Insecure dependency in socketpair");
  2157. #endif
  2158. #ifdef HAS_SOCKETPAIR
  2159.     if (socketpair(domain,type,protocol,fd) < 0)
  2160.     return FALSE;
  2161. #else
  2162.     fatal("Socketpair unimplemented");
  2163. #endif
  2164.     stio1->ifp = fdopen(fd[0], "r");
  2165.     stio1->ofp = fdopen(fd[0], "w");
  2166.     stio1->type = 's';
  2167.     stio2->ifp = fdopen(fd[1], "r");
  2168.     stio2->ofp = fdopen(fd[1], "w");
  2169.     stio2->type = 's';
  2170.     if (!stio1->ifp || !stio1->ofp || !stio2->ifp || !stio2->ofp) {
  2171.     if (stio1->ifp) fclose(stio1->ifp);
  2172.     if (stio1->ofp) fclose(stio1->ofp);
  2173.     if (!stio1->ifp && !stio1->ofp) close(fd[0]);
  2174.     if (stio2->ifp) fclose(stio2->ifp);
  2175.     if (stio2->ofp) fclose(stio2->ofp);
  2176.     if (!stio2->ifp && !stio2->ofp) close(fd[1]);
  2177.     return FALSE;
  2178.     }
  2179.  
  2180.     return TRUE;
  2181. }
  2182.  
  2183. #endif /* HAS_SOCKET */
  2184.  
  2185. int
  2186. do_gpwent(which,gimme,arglast)
  2187. int which;
  2188. int gimme;
  2189. int *arglast;
  2190. {
  2191. #ifdef HAS_PASSWD
  2192.     register ARRAY *ary = stack;
  2193.     register int sp = arglast[0];
  2194.     register STR *str;
  2195.     struct passwd *getpwnam();
  2196.     struct passwd *getpwuid();
  2197.     struct passwd *getpwent();
  2198.     struct passwd *pwent;
  2199.  
  2200.     if (which == O_GPWNAM) {
  2201.     char *name = str_get(ary->ary_array[sp+1]);
  2202.  
  2203.     pwent = getpwnam(name);
  2204.     }
  2205.     else if (which == O_GPWUID) {
  2206.     int uid = (int)str_gnum(ary->ary_array[sp+1]);
  2207.  
  2208.     pwent = getpwuid(uid);
  2209.     }
  2210.     else
  2211.     pwent = getpwent();
  2212.  
  2213.     if (gimme != G_ARRAY) {
  2214.     astore(ary, ++sp, str = str_mortal(&str_undef));
  2215.     if (pwent) {
  2216.         if (which == O_GPWNAM)
  2217.         str_numset(str, (double)pwent->pw_uid);
  2218.         else
  2219.         str_set(str, pwent->pw_name);
  2220.     }
  2221.     return sp;
  2222.     }
  2223.  
  2224.     if (pwent) {
  2225.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2226.     str_set(str, pwent->pw_name);
  2227.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2228.     str_set(str, pwent->pw_passwd);
  2229.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2230.     str_numset(str, (double)pwent->pw_uid);
  2231.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2232.     str_numset(str, (double)pwent->pw_gid);
  2233.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2234. #ifdef PWCHANGE
  2235.     str_numset(str, (double)pwent->pw_change);
  2236. #else
  2237. #ifdef PWQUOTA
  2238.     str_numset(str, (double)pwent->pw_quota);
  2239. #else
  2240. #ifdef PWAGE
  2241.     str_set(str, pwent->pw_age);
  2242. #endif
  2243. #endif
  2244. #endif
  2245.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2246. #ifdef PWCLASS
  2247.     str_set(str,pwent->pw_class);
  2248. #else
  2249. #ifdef PWCOMMENT
  2250.     str_set(str, pwent->pw_comment);
  2251. #endif
  2252. #endif
  2253.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2254.     str_set(str, pwent->pw_gecos);
  2255.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2256.     str_set(str, pwent->pw_dir);
  2257.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2258.     str_set(str, pwent->pw_shell);
  2259. #ifdef PWEXPIRE
  2260.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2261.     str_numset(str, (double)pwent->pw_expire);
  2262. #endif
  2263.     }
  2264.  
  2265.     return sp;
  2266. #else
  2267.     fatal("password routines not implemented");
  2268. #endif
  2269. }
  2270.  
  2271. int
  2272. do_ggrent(which,gimme,arglast)
  2273. int which;
  2274. int gimme;
  2275. int *arglast;
  2276. {
  2277. #ifdef I_GRP
  2278.     register ARRAY *ary = stack;
  2279.     register int sp = arglast[0];
  2280.     register char **elem;
  2281.     register STR *str;
  2282.     struct group *getgrnam();
  2283.     struct group *getgrgid();
  2284.     struct group *getgrent();
  2285.     struct group *grent;
  2286.  
  2287.     if (which == O_GGRNAM) {
  2288.     char *name = str_get(ary->ary_array[sp+1]);
  2289.  
  2290.     grent = getgrnam(name);
  2291.     }
  2292.     else if (which == O_GGRGID) {
  2293.     int gid = (int)str_gnum(ary->ary_array[sp+1]);
  2294.  
  2295.     grent = getgrgid(gid);
  2296.     }
  2297.     else
  2298.     grent = getgrent();
  2299.  
  2300.     if (gimme != G_ARRAY) {
  2301.     astore(ary, ++sp, str = str_mortal(&str_undef));
  2302.     if (grent) {
  2303.         if (which == O_GGRNAM)
  2304.         str_numset(str, (double)grent->gr_gid);
  2305.         else
  2306.         str_set(str, grent->gr_name);
  2307.     }
  2308.     return sp;
  2309.     }
  2310.  
  2311.     if (grent) {
  2312.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2313.     str_set(str, grent->gr_name);
  2314.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2315.     str_set(str, grent->gr_passwd);
  2316.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2317.     str_numset(str, (double)grent->gr_gid);
  2318.     (void)astore(ary, ++sp, str = str_mortal(&str_no));
  2319.     for (elem = grent->gr_mem; *elem; elem++) {
  2320.         str_cat(str, *elem);
  2321.         if (elem[1])
  2322.         str_ncat(str," ",1);
  2323.     }
  2324.     }
  2325.  
  2326.     return sp;
  2327. #else
  2328.     fatal("group routines not implemented");
  2329. #endif
  2330. }
  2331.  
  2332. int
  2333. do_dirop(optype,stab,gimme,arglast)
  2334. int optype;
  2335. STAB *stab;
  2336. int gimme;
  2337. int *arglast;
  2338. {
  2339. #if defined(DIRENT) && defined(HAS_READDIR)
  2340.     register ARRAY *ary = stack;
  2341.     register STR **st = ary->ary_array;
  2342.     register int sp = arglast[1];
  2343.     register STIO *stio;
  2344.     long along;
  2345. #ifndef apollo
  2346.     struct DIRENT *readdir();
  2347. #endif
  2348.     register struct DIRENT *dp;
  2349.  
  2350.     if (!stab)
  2351.     goto nope;
  2352.     if (!(stio = stab_io(stab)))
  2353.     stio = stab_io(stab) = stio_new();
  2354.     if (!stio->dirp && optype != O_OPEN_DIR)
  2355.     goto nope;
  2356.     st[sp] = &str_yes;
  2357.     switch (optype) {
  2358.     case O_OPEN_DIR:
  2359.     if (stio->dirp)
  2360.         closedir(stio->dirp);
  2361.     if (!(stio->dirp = opendir(str_get(st[sp+1]))))
  2362.         goto nope;
  2363.     break;
  2364.     case O_READDIR:
  2365.     if (gimme == G_ARRAY) {
  2366.         --sp;
  2367.         /*SUPPRESS 560*/
  2368.         while (dp = readdir(stio->dirp)) {
  2369. #ifdef DIRNAMLEN
  2370.         (void)astore(ary,++sp,
  2371.           str_2mortal(str_make(dp->d_name,dp->d_namlen)));
  2372. #else
  2373.         (void)astore(ary,++sp,
  2374.           str_2mortal(str_make(dp->d_name,0)));
  2375. #endif
  2376.         }
  2377.     }
  2378.     else {
  2379.         if (!(dp = readdir(stio->dirp)))
  2380.         goto nope;
  2381.         st[sp] = str_mortal(&str_undef);
  2382. #ifdef DIRNAMLEN
  2383.         str_nset(st[sp], dp->d_name, dp->d_namlen);
  2384. #else
  2385.         str_set(st[sp], dp->d_name);
  2386. #endif
  2387.     }
  2388.     break;
  2389. #if defined(HAS_TELLDIR) || defined(telldir)
  2390.     case O_TELLDIR: {
  2391. #ifndef telldir
  2392.         long telldir();
  2393. #endif
  2394.         st[sp] = str_mortal(&str_undef);
  2395.         str_numset(st[sp], (double)telldir(stio->dirp));
  2396.         break;
  2397.     }
  2398. #endif
  2399. #if defined(HAS_SEEKDIR) || defined(seekdir)
  2400.     case O_SEEKDIR:
  2401.     st[sp] = str_mortal(&str_undef);
  2402.     along = (long)str_gnum(st[sp+1]);
  2403.     (void)seekdir(stio->dirp,along);
  2404.     break;
  2405. #endif
  2406. #if defined(HAS_REWINDDIR) || defined(rewinddir)
  2407.     case O_REWINDDIR:
  2408.     st[sp] = str_mortal(&str_undef);
  2409.     (void)rewinddir(stio->dirp);
  2410.     break;
  2411. #endif
  2412.     case O_CLOSEDIR:
  2413.     st[sp] = str_mortal(&str_undef);
  2414.     (void)closedir(stio->dirp);
  2415.     stio->dirp = 0;
  2416.     break;
  2417.     default:
  2418.     goto phooey;
  2419.     }
  2420.     return sp;
  2421.  
  2422. nope:
  2423.     st[sp] = &str_undef;
  2424.     if (!errno)
  2425.     errno = EBADF;
  2426.     return sp;
  2427.  
  2428. #endif
  2429. phooey:
  2430.     fatal("Unimplemented directory operation");
  2431. }
  2432.  
  2433. int
  2434. apply(type,arglast)
  2435. int type;
  2436. int *arglast;
  2437. {
  2438.     register STR **st = stack->ary_array;
  2439.     register int sp = arglast[1];
  2440.     register int items = arglast[2] - sp;
  2441.     register int val;
  2442.     register int val2;
  2443.     register int tot = 0;
  2444.     char *s;
  2445.  
  2446. #ifdef TAINT
  2447.     for (st += ++sp; items--; st++)
  2448.     tainted |= (*st)->str_tainted;
  2449.     st = stack->ary_array;
  2450.     sp = arglast[1];
  2451.     items = arglast[2] - sp;
  2452. #endif
  2453.     switch (type) {
  2454.     case O_CHMOD:
  2455. #ifdef TAINT
  2456.     taintproper("Insecure dependency in chmod");
  2457. #endif
  2458.     if (--items > 0) {
  2459.         tot = items;
  2460.         val = (int)str_gnum(st[++sp]);
  2461.         while (items--) {
  2462.         if (chmod(str_get(st[++sp]),val))
  2463.             tot--;
  2464.         }
  2465.     }
  2466.     break;
  2467. #ifdef HAS_CHOWN
  2468.     case O_CHOWN:
  2469. #ifdef TAINT
  2470.     taintproper("Insecure dependency in chown");
  2471. #endif
  2472.     if (items > 2) {
  2473.         items -= 2;
  2474.         tot = items;
  2475.         val = (int)str_gnum(st[++sp]);
  2476.         val2 = (int)str_gnum(st[++sp]);
  2477.         while (items--) {
  2478.         if (chown(str_get(st[++sp]),val,val2))
  2479.             tot--;
  2480.         }
  2481.     }
  2482.     break;
  2483. #endif
  2484. #ifdef HAS_KILL
  2485.     case O_KILL:
  2486. #ifdef TAINT
  2487.     taintproper("Insecure dependency in kill");
  2488. #endif
  2489.     if (--items > 0) {
  2490.         tot = items;
  2491.         s = str_get(st[++sp]);
  2492.         if (isUPPER(*s)) {
  2493.         if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
  2494.             s += 3;
  2495.         if (!(val = whichsig(s)))
  2496.             fatal("Unrecognized signal name \"%s\"",s);
  2497.         }
  2498.         else
  2499.         val = (int)str_gnum(st[sp]);
  2500.         if (val < 0) {
  2501.         val = -val;
  2502.         while (items--) {
  2503.             int proc = (int)str_gnum(st[++sp]);
  2504. #ifdef HAS_KILLPG
  2505.             if (killpg(proc,val))    /* BSD */
  2506. #else
  2507.             if (kill(-proc,val))    /* SYSV */
  2508. #endif
  2509.             tot--;
  2510.         }
  2511.         }
  2512.         else {
  2513.         while (items--) {
  2514.             if (kill((int)(str_gnum(st[++sp])),val))
  2515.             tot--;
  2516.         }
  2517.         }
  2518.     }
  2519.     break;
  2520. #endif
  2521.     case O_UNLINK:
  2522. #ifdef TAINT
  2523.     taintproper("Insecure dependency in unlink");
  2524. #endif
  2525.     tot = items;
  2526.     while (items--) {
  2527.         s = str_get(st[++sp]);
  2528.         if (euid || unsafe) {
  2529.         if (UNLINK(s))
  2530.             tot--;
  2531.         }
  2532.         else {    /* don't let root wipe out directories without -U */
  2533. #ifdef HAS_LSTAT
  2534.         if (lstat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
  2535. #else
  2536.         if (stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
  2537. #endif
  2538.             tot--;
  2539.         else {
  2540.             if (UNLINK(s))
  2541.             tot--;
  2542.         }
  2543.         }
  2544.     }
  2545.     break;
  2546.     case O_UTIME:
  2547. #ifdef TAINT
  2548.     taintproper("Insecure dependency in utime");
  2549. #endif
  2550.     if (items > 2) {
  2551. #ifdef I_UTIME
  2552.         struct utimbuf utbuf;
  2553. #else
  2554.         struct {
  2555.         long    actime;
  2556.         long    modtime;
  2557.         } utbuf;
  2558. #endif
  2559.  
  2560.         Zero(&utbuf, sizeof utbuf, char);
  2561.         utbuf.actime = (long)str_gnum(st[++sp]);    /* time accessed */
  2562.         utbuf.modtime = (long)str_gnum(st[++sp]);    /* time modified */
  2563.         items -= 2;
  2564. #ifndef lint
  2565.         tot = items;
  2566.         while (items--) {
  2567.         if (utime(str_get(st[++sp]),&utbuf))
  2568.             tot--;
  2569.         }
  2570. #endif
  2571.     }
  2572.     else
  2573.         items = 0;
  2574.     break;
  2575.     }
  2576.     return tot;
  2577. }
  2578.  
  2579. /* Do the permissions allow some operation?  Assumes statcache already set. */
  2580.  
  2581. int
  2582. cando(bit, effective, statbufp)
  2583. int bit;
  2584. int effective;
  2585. register struct stat *statbufp;
  2586. {
  2587. #ifdef DOSISH
  2588.     /* [Comments and code from Len Reed]
  2589.      * MS-DOS "user" is similar to UNIX's "superuser," but can't write
  2590.      * to write-protected files.  The execute permission bit is set
  2591.      * by the Miscrosoft C library stat() function for the following:
  2592.      *        .exe files
  2593.      *        .com files
  2594.      *        .bat files
  2595.      *        directories
  2596.      * All files and directories are readable.
  2597.      * Directories and special files, e.g. "CON", cannot be
  2598.      * write-protected.
  2599.      * [Comment by Tom Dinger -- a directory can have the write-protect
  2600.      *        bit set in the file system, but DOS permits changes to
  2601.      *        the directory anyway.  In addition, all bets are off
  2602.      *        here for networked software, such as Novell and
  2603.      *        Sun's PC-NFS.]
  2604.      */
  2605.  
  2606.      /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
  2607.       * too so it will actually look into the files for magic numbers
  2608.       */
  2609.      return (bit & statbufp->st_mode) ? TRUE : FALSE;
  2610.  
  2611. #else /* ! MSDOS */
  2612.     if ((effective ? euid : uid) == 0) {    /* root is special */
  2613.     if (bit == S_IXUSR) {
  2614.         if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
  2615.         return TRUE;
  2616.     }
  2617.     else
  2618.         return TRUE;        /* root reads and writes anything */
  2619.     return FALSE;
  2620.     }
  2621.     if (statbufp->st_uid == (effective ? euid : uid) ) {
  2622.     if (statbufp->st_mode & bit)
  2623.         return TRUE;    /* ok as "user" */
  2624.     }
  2625.     else if (ingroup((int)statbufp->st_gid,effective)) {
  2626.     if (statbufp->st_mode & bit >> 3)
  2627.         return TRUE;    /* ok as "group" */
  2628.     }
  2629.     else if (statbufp->st_mode & bit >> 6)
  2630.     return TRUE;    /* ok as "other" */
  2631.     return FALSE;
  2632. #endif /* ! MSDOS */
  2633. }
  2634.  
  2635. int
  2636. ingroup(testgid,effective)
  2637. int testgid;
  2638. int effective;
  2639. {
  2640.     if (testgid == (effective ? egid : gid))
  2641.     return TRUE;
  2642. #ifdef HAS_GETGROUPS
  2643. #ifndef NGROUPS
  2644. #define NGROUPS 32
  2645. #endif
  2646.     {
  2647.     GROUPSTYPE gary[NGROUPS];
  2648.     int anum;
  2649.  
  2650.     anum = getgroups(NGROUPS,gary);
  2651.     while (--anum >= 0)
  2652.         if (gary[anum] == testgid)
  2653.         return TRUE;
  2654.     }
  2655. #endif
  2656.     return FALSE;
  2657. }
  2658.  
  2659. #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
  2660.  
  2661. int
  2662. do_ipcget(optype, arglast)
  2663. int optype;
  2664. int *arglast;
  2665. {
  2666.     register STR **st = stack->ary_array;
  2667.     register int sp = arglast[0];
  2668.     key_t key;
  2669.     int n, flags;
  2670.  
  2671.     key = (key_t)str_gnum(st[++sp]);
  2672.     n = (optype == O_MSGGET) ? 0 : (int)str_gnum(st[++sp]);
  2673.     flags = (int)str_gnum(st[++sp]);
  2674.     errno = 0;
  2675.     switch (optype)
  2676.     {
  2677. #ifdef HAS_MSG
  2678.     case O_MSGGET:
  2679.     return msgget(key, flags);
  2680. #endif
  2681. #ifdef HAS_SEM
  2682.     case O_SEMGET:
  2683.     return semget(key, n, flags);
  2684. #endif
  2685. #ifdef HAS_SHM
  2686.     case O_SHMGET:
  2687.     return shmget(key, n, flags);
  2688. #endif
  2689. #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
  2690.     default:
  2691.     fatal("%s not implemented", opname[optype]);
  2692. #endif
  2693.     }
  2694.     return -1;            /* should never happen */
  2695. }
  2696.  
  2697. int
  2698. do_ipcctl(optype, arglast)
  2699. int optype;
  2700. int *arglast;
  2701. {
  2702.     register STR **st = stack->ary_array;
  2703.     register int sp = arglast[0];
  2704.     STR *astr;
  2705.     char *a;
  2706.     int id, n, cmd, infosize, getinfo, ret;
  2707.  
  2708.     id = (int)str_gnum(st[++sp]);
  2709.     n = (optype == O_SEMCTL) ? (int)str_gnum(st[++sp]) : 0;
  2710.     cmd = (int)str_gnum(st[++sp]);
  2711.     astr = st[++sp];
  2712.  
  2713.     infosize = 0;
  2714.     getinfo = (cmd == IPC_STAT);
  2715.  
  2716.     switch (optype)
  2717.     {
  2718. #ifdef HAS_MSG
  2719.     case O_MSGCTL:
  2720.     if (cmd == IPC_STAT || cmd == IPC_SET)
  2721.         infosize = sizeof(struct msqid_ds);
  2722.     break;
  2723. #endif
  2724. #ifdef HAS_SHM
  2725.     case O_SHMCTL:
  2726.     if (cmd == IPC_STAT || cmd == IPC_SET)
  2727.         infosize = sizeof(struct shmid_ds);
  2728.     break;
  2729. #endif
  2730. #ifdef HAS_SEM
  2731.     case O_SEMCTL:
  2732.     if (cmd == IPC_STAT || cmd == IPC_SET)
  2733.         infosize = sizeof(struct semid_ds);
  2734.     else if (cmd == GETALL || cmd == SETALL)
  2735.     {
  2736.         struct semid_ds semds;
  2737.         if (semctl(id, 0, IPC_STAT, &semds) == -1)
  2738.         return -1;
  2739.         getinfo = (cmd == GETALL);
  2740.         infosize = semds.sem_nsems * sizeof(short);
  2741.         /* "short" is technically wrong but much more portable
  2742.            than guessing about u_?short(_t)? */
  2743.     }
  2744.     break;
  2745. #endif
  2746. #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
  2747.     default:
  2748.     fatal("%s not implemented", opname[optype]);
  2749. #endif
  2750.     }
  2751.  
  2752.     if (infosize)
  2753.     {
  2754.     if (getinfo)
  2755.     {
  2756.         STR_GROW(astr, infosize+1);
  2757.         a = str_get(astr);
  2758.     }
  2759.     else
  2760.     {
  2761.         a = str_get(astr);
  2762.         if (astr->str_cur != infosize)
  2763.         {
  2764.         errno = EINVAL;
  2765.         return -1;
  2766.         }
  2767.     }
  2768.     }
  2769.     else
  2770.     {
  2771.     int i = (int)str_gnum(astr);
  2772.     a = (char *)i;        /* ouch */
  2773.     }
  2774.     errno = 0;
  2775.     switch (optype)
  2776.     {
  2777. #ifdef HAS_MSG
  2778.     case O_MSGCTL:
  2779.     ret = msgctl(id, cmd, (struct msqid_ds *)a);
  2780.     break;
  2781. #endif
  2782. #ifdef HAS_SEM
  2783.     case O_SEMCTL:
  2784.     ret = semctl(id, n, cmd, a);
  2785.     break;
  2786. #endif
  2787. #ifdef HAS_SHM
  2788.     case O_SHMCTL:
  2789.     ret = shmctl(id, cmd, (struct shmid_ds *)a);
  2790.     break;
  2791. #endif
  2792.     }
  2793.     if (getinfo && ret >= 0) {
  2794.     astr->str_cur = infosize;
  2795.     astr->str_ptr[infosize] = '\0';
  2796.     }
  2797.     return ret;
  2798. }
  2799.  
  2800. int
  2801. do_msgsnd(arglast)
  2802. int *arglast;
  2803. {
  2804. #ifdef HAS_MSG
  2805.     register STR **st = stack->ary_array;
  2806.     register int sp = arglast[0];
  2807.     STR *mstr;
  2808.     char *mbuf;
  2809.     int id, msize, flags;
  2810.  
  2811.     id = (int)str_gnum(st[++sp]);
  2812.     mstr = st[++sp];
  2813.     flags = (int)str_gnum(st[++sp]);
  2814.     mbuf = str_get(mstr);
  2815.     if ((msize = mstr->str_cur - sizeof(long)) < 0) {
  2816.     errno = EINVAL;
  2817.     return -1;
  2818.     }
  2819.     errno = 0;
  2820.     return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
  2821. #else
  2822.     fatal("msgsnd not implemented");
  2823. #endif
  2824. }
  2825.  
  2826. int
  2827. do_msgrcv(arglast)
  2828. int *arglast;
  2829. {
  2830. #ifdef HAS_MSG
  2831.     register STR **st = stack->ary_array;
  2832.     register int sp = arglast[0];
  2833.     STR *mstr;
  2834.     char *mbuf;
  2835.     long mtype;
  2836.     int id, msize, flags, ret;
  2837.  
  2838.     id = (int)str_gnum(st[++sp]);
  2839.     mstr = st[++sp];
  2840.     msize = (int)str_gnum(st[++sp]);
  2841.     mtype = (long)str_gnum(st[++sp]);
  2842.     flags = (int)str_gnum(st[++sp]);
  2843.     mbuf = str_get(mstr);
  2844.     if (mstr->str_cur < sizeof(long)+msize+1) {
  2845.     STR_GROW(mstr, sizeof(long)+msize+1);
  2846.     mbuf = str_get(mstr);
  2847.     }
  2848.     errno = 0;
  2849.     ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
  2850.     if (ret >= 0) {
  2851.     mstr->str_cur = sizeof(long)+ret;
  2852.     mstr->str_ptr[sizeof(long)+ret] = '\0';
  2853.     }
  2854.     return ret;
  2855. #else
  2856.     fatal("msgrcv not implemented");
  2857. #endif
  2858. }
  2859.  
  2860. int
  2861. do_semop(arglast)
  2862. int *arglast;
  2863. {
  2864. #ifdef HAS_SEM
  2865.     register STR **st = stack->ary_array;
  2866.     register int sp = arglast[0];
  2867.     STR *opstr;
  2868.     char *opbuf;
  2869.     int id, opsize;
  2870.  
  2871.     id = (int)str_gnum(st[++sp]);
  2872.     opstr = st[++sp];
  2873.     opbuf = str_get(opstr);
  2874.     opsize = opstr->str_cur;
  2875.     if (opsize < sizeof(struct sembuf)
  2876.     || (opsize % sizeof(struct sembuf)) != 0) {
  2877.     errno = EINVAL;
  2878.     return -1;
  2879.     }
  2880.     errno = 0;
  2881.     return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
  2882. #else
  2883.     fatal("semop not implemented");
  2884. #endif
  2885. }
  2886.  
  2887. int
  2888. do_shmio(optype, arglast)
  2889. int optype;
  2890. int *arglast;
  2891. {
  2892. #ifdef HAS_SHM
  2893.     register STR **st = stack->ary_array;
  2894.     register int sp = arglast[0];
  2895.     STR *mstr;
  2896.     char *mbuf, *shm;
  2897.     int id, mpos, msize;
  2898.     struct shmid_ds shmds;
  2899. #ifndef VOIDSHMAT
  2900.     extern char *shmat();
  2901. #endif
  2902.  
  2903.     id = (int)str_gnum(st[++sp]);
  2904.     mstr = st[++sp];
  2905.     mpos = (int)str_gnum(st[++sp]);
  2906.     msize = (int)str_gnum(st[++sp]);
  2907.     errno = 0;
  2908.     if (shmctl(id, IPC_STAT, &shmds) == -1)
  2909.     return -1;
  2910.     if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
  2911.     errno = EFAULT;        /* can't do as caller requested */
  2912.     return -1;
  2913.     }
  2914.     shm = (char*)shmat(id, (char*)NULL, (optype == O_SHMREAD) ? SHM_RDONLY : 0);
  2915.     if (shm == (char *)-1)    /* I hate System V IPC, I really do */
  2916.     return -1;
  2917.     mbuf = str_get(mstr);
  2918.     if (optype == O_SHMREAD) {
  2919.     if (mstr->str_cur < msize) {
  2920.         STR_GROW(mstr, msize+1);
  2921.         mbuf = str_get(mstr);
  2922.     }
  2923.     Copy(shm + mpos, mbuf, msize, char);
  2924.     mstr->str_cur = msize;
  2925.     mstr->str_ptr[msize] = '\0';
  2926.     }
  2927.     else {
  2928.     int n;
  2929.  
  2930.     if ((n = mstr->str_cur) > msize)
  2931.         n = msize;
  2932.     Copy(mbuf, shm + mpos, n, char);
  2933.     if (n < msize)
  2934.         memzero(shm + mpos + n, msize - n);
  2935.     }
  2936.     return shmdt(shm);
  2937. #else
  2938.     fatal("shm I/O not implemented");
  2939. #endif
  2940. }
  2941.  
  2942. #endif /* SYSV IPC */
  2943.